table.insert(table.table2.table3, "Stuff")
or like this:
table.insert(table.table2, "Stuff") – Where i would be changing?
Please help me if you can, thanks.
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.
Well, I couldn't find a good way to word it. I do know about making tables within tables. But I would like to make tables inside tables with a Lua command. This is because what would be inside a table would not be a 'set' value. What I am looking for is a way to make a currently non existing table, exist. But the table would have a table inside it with data that I wouldn't know. Here is an example:That is entirely unrelated to the question as asked.
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.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.
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
MainMenu = {
[1] = { Option = "Hello", ProgramName = "Hello.lua" },
[2] = { Option = "Goodbye", ProgramName = "Goodbye.lua }
}
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
-- 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
That isn't what I asked for, but this will help me with other things. Thanks for the feedback!I don't have much experience with tables, but my guess would be the textutils.serialize() and textutils.unserialize() functions. Serialize turns the table into a string, and unserialize switches it back into a table. This is also useful for sending tables over rednet, as rednet.send() only works with strings.
Hope I helped!
The above code is the entire code. The variable "ProgramMenu" is actually a CREATED table, and if you look in where I pasted the "CreateMenu()" function, it shows where it gets the info from the fs.list(). I figured table.insert would also create a table. But, that is why I am here, is to figure out how I can create a non existing table, because as I have mentioned before is I could only create the first table. I can't create the second table (The table inside the first) Because I wouldn't have a 'set' number of those. Look below:Okay, well I do not see you ever declare the variable ProgramMenu in your code, which would be why it is giving you that particular error. I don't know if that is because you just did forgot to paste it, but just in case could you please provide the entire code rather than just separated pieces? It's difficult to tell what you are trying to do when the code is separated like that.
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.
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
Right, but you're only inserting data into the first table, when I want to insert data into a table INSIDE the first table. Like this:lets say like this: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.
}
test = {{},{},{}}
would actually work for example. these 3 tables would be refered as test[1], test[2] and test[3]I'd like to know how to create a non declared table. The reason I can't declare the tables is because I don't know how many there would be inside the first table, such as the number of programs. Isn't there a table.create()? For creating tables to insert data into?Of course you can create them; you do it inside the loop. ProgramMenu = {}. Then you fill values. You do need to declare ProgramMenu at some point, though. Just make sure to declare the table if there isn't already one before you try to fill it with values.
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
Perhaps it was hard to get what I needed across… It seems all I needed was the ProgramMenu = {}, thanks for you help!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?
Yeah, in a sense he did. But I took it as him declaring the tables himself, instead of with a function.Or just read the replies, Lyqyd gave you that in the second reply.