#1
Posted 04 December 2012 - 02:08 PM
table.insert(table.table2.table3, "Stuff")
or like this:
table.insert(table[i].table2, "Stuff") -- Where i would be changing?
Please help me if you can, thanks.
#2
Posted 04 December 2012 - 03:04 PM
Hope I helped!
#3
Posted 04 December 2012 - 03:11 PM
Tables within tables are easy:
aTable = {} --declare a table. aTable[1] = {} --declare a table at index 1 of aTable. aTable[1][1] = "bob" -set the value of the first index of the table stored in the first index of aTable.
#4
Posted 04 December 2012 - 05:29 PM
Lyqyd, on 04 December 2012 - 03:11 PM, said:
Tables within tables are easy:
aTable = {} --declare a table. aTable[1] = {} --declare a table at index 1 of aTable. aTable[1][1] = "bob" -set the value of the first index of the table stored in the first index of aTable.
ProgramList = { [1] = { ProgramDirectory = "Program1", ProgramName = "Program1.lua" }, [2] = { ProgramDirectory = "Program2", ProgramName = "Program2.lua" } }I am wondering how to be able to create that type of table without actually knowing what would go in it. I have tried a lot of different code and can't seem to get it to work. Please help if you can. Also, I'd prefer to use that layou if possible. Thanks in advance.
#5
Posted 04 December 2012 - 07:07 PM
#6
Posted 04 December 2012 - 07:32 PM
#7
Posted 04 December 2012 - 09:15 PM
local usercreatedtable = {} repeat write("Enter a new subtable name: ") local stname = read() usercreatedtable[stname] = {} write("Enter new subtable size: ") local stsize = tonumber(read()) for i = 1,stsize do write("Enter subtable entry "..i..": ") usercreatedtable[stname][i] = read() end write("Do you wish to enter additional subtables?") until read() == "no"Just so that you can see what Lyqyd proposed in context of being used with unknown input, not that this is likely useful code. Now, here we should end up with a table of subtables that have user entered names, user entered sizes, and user entered data in them.
#8
Posted 05 December 2012 - 04:13 AM
function PrintMenu(menu) for i = 1, #menu do if i == SelectedItem then term.setTextColor(colors.yellow) print(menu[i].option) -- Option in this case would be the name of the folder the program is in. term.setTextColor(colors.white) else term.setTextColor(colors.gray) print(menu[i].option) term.setTextColor(colors.white) end end end function KeyHandler(key, menu) if key == 28 then UseOption(menu) elseif key == 200 then if SelectedItem > 1 then SelectedItem = SelectedItem - 1 else SelectedItem = #menu end elseif key == 208 then if SelectedItem < #menu then SelectedItem = SelectedItem + 1 else SelectedItem = 1 end end end function UseOption(menu) shell.run(menu[SelectedItem].Option .. "/" .. menu[SelectedItem].ProgramName) end
That is the code to print and handle the menu. The menu that I want to create is one that contains a fs.list(). This is how the default menus look that those functions handle:
MainMenu = { [1] = { Option = "Hello", ProgramName = "Hello.lua" }, [2] = { Option = "Goodbye", ProgramName = "Goodbye.lua } }
The "Option" In that menu would be the directory that the program is in (It would be in a directory for a reason...) and the "ProgramName" is the name of the program inside the folder that should be ran when that option is selected. The only difference between the name of the directory and the program name is a ".lua" after the "ProgramName".
What I wanted to do is have a menu creating function. Which would set up the table for the menu, as it would not already exist, and I can't create it because I don't know how many programs it would create so I can't create the second table, with [#]'s. This is how I wanted to do that.
function CreateMenu() ProgramList = fs.list("Programs") i = 1 for k, v in pairs(ProgramList) do if fs.isDir(v) then table.insert(ProgramMenu[i].Option, v) table.insert(ProgramMenu[i].ProgramName, v .. ".lua") i = i + 1 end end end
The way that it I thought it would work:
-- Pretend all the code from above is in here... local UseProgramMenu = false local SelectedItem = 1 local InMain = true function Main() CreateMenu() SelectedItem = 1 -- Put in here again just in case... UseProgramMenu = true while UseProgramMenu do term.clear() term.setCursorPos(1, 1) print("Program Menu\n") PrintMenu(ProgramMenu) event, key = os.pullEvent("key") KeyHandler(key, ProgramMenu) end end while InMain do Main() end
You may not need all the code from above, but that is all that it should need to be. I have tried this code and I get an error in the "PrintMenu()" function on the line of the "for" loop. Where it says "for i = 1, #menu do" -- Attempt to index a nil value, or something like that. Anyways. Please help me out if you can, that is how I thought it would work.
#9
Posted 05 December 2012 - 04:15 AM
Dlcruz129, on 04 December 2012 - 03:04 PM, said:
Hope I helped!
#10
Posted 05 December 2012 - 04:20 AM
What you are trying to do, however, is entirely possible. You can have as many tables within tables as you want (within reason that is).
#11
Posted 05 December 2012 - 04:31 AM
Bubba, on 05 December 2012 - 04:20 AM, said:
What you are trying to do, however, is entirely possible. You can have as many tables within tables as you want (within reason that is).
ProgramMenu = { [1] = { Option = "Hello", ProgramName = "Hello.lua" }, [2] = { Option = "Goodbye", ProgramName = "Goodbye.lua" } }Where the [1], and [2] are, those are the tables inside the tables. If you can see what I mean, each program would have its own "[#]", and I don't know how many programs there would be, because that could change, so I can't create those tables, I have to have the function "CreateMenu()" do it. The only thing I would be able to create is "ProgramMenu = {}", the other stuff would have to be CREATED by the function. I need help with the function to create it.
(I also would like to keep that layout, rather than the table created by fs.list() )
Sorry if that doesn't make sense, I still cannot figure out a good way to word it... XD
#12
Posted 05 December 2012 - 04:36 AM
words = {} -- I make an empty table. while true do -- Start a while true loop input = read() -- Read something table.insert(words, input) -- I insert input into the table words for i, q in pairs(words) do -- start a loop of all the values in words print(q) -- print the value currently selected end -- end the loop input = "" -- set input equal to nothing end -- and end the while true loop
#13
Posted 05 December 2012 - 04:40 AM
Doyle3694, on 05 December 2012 - 04:36 AM, said:
words = {} -- I make an empty table. while true do -- Start a while true loop input = read() -- Read something table.insert(words, input) -- I insert input into the table words for i in pairs words do -- start a loop of all the values in words print(i) -- print the value currently selected end -- end the loop input = "" -- set input equal to nothing end -- and end the while true loop
ProgramMenu = { [1] = {} -- CREATE, and then INSERT the data into table [1]. This table would be the information for the first discovered program. }
Also, I can't create the [1], [2], [3], ects... Because I don't know how many programs there would be...
#14
Posted 05 December 2012 - 04:48 AM
test = {{},{},{}}would actually work for example. these 3 tables would be refered as test[1], test[2] and test[3]
#15
Posted 05 December 2012 - 04:50 AM
#16
Posted 05 December 2012 - 04:55 AM
Lyqyd, on 05 December 2012 - 04:50 AM, said:
#17
Posted 05 December 2012 - 05:08 AM
local ProgramMenu = {} --declare this table function CreateMenu() ProgramList = fs.list("Programs") i = 1 for k, v in pairs(ProgramList) do if fs.isDir(v) then ProgramMenu[i] = {} --declare the table when you need it ProgramMenu[i].Option = v --insert these as values rather than trying to use table.insert ProgramMenu[i].ProgramName = v .. ".lua" i = i + 1 end end end
Was that really that hard?
#18
Posted 05 December 2012 - 05:10 AM
Lyqyd, on 05 December 2012 - 05:08 AM, said:
local ProgramMenu = {} --declare this table function CreateMenu() ProgramList = fs.list("Programs") i = 1 for k, v in pairs(ProgramList) do if fs.isDir(v) then ProgramMenu[i] = {} --declare the table when you need it ProgramMenu[i].Option = v --insert these as values rather than trying to use table.insert ProgramMenu[i].ProgramName = v .. ".lua" i = i + 1 end end end
Was that really that hard?
#19
Posted 05 December 2012 - 05:11 AM
#20
Posted 05 December 2012 - 10:36 AM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users