This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

Table in a Table

Started by FuuuAInfiniteLoop(F.A.I.L), 18 March 2013 - 07:45 AM
FuuuAInfiniteLoop(F.A.I.L) #1
Posted 18 March 2013 - 08:45 AM
I want to create a table that can be called like table1[table2][table3][name] = value

is that possible? And if its possible, i can serialize and unsrialize that table(because i want to store in a file)?
Bubba #2
Posted 18 March 2013 - 08:48 AM
Yes and yes. Just make sure that you have defined the table within your table before trying to add values to it.


local myTable = {}
myTable[1] = {}
myTable[2] = {}
myTable[1][1] = "Hello!"
myTable[2][1] = "Test!"

Or a cleaner version:

local myTable = {
  ["table1"] = {1,2,3,"HELLO!"};
  ["table2"] = {3,2,1,"Test!!!"};
  [3] = {"whatever", 1,2,3};
}

print(myTable.table1[1]) --Outputs 1
print(myTable.table2[4]) --Outputs "Test!!!"
print(myTable[3][1]) --Outputs "whatever"
FuuuAInfiniteLoop(F.A.I.L) #3
Posted 18 March 2013 - 08:52 AM
thanks
SuicidalSTDz #4
Posted 18 March 2013 - 09:07 AM
table = {
["someTable"] = {
  {text = "something here"},
  {text = "more things"},
  {text = "last thing"}
},
["someOtherTable"] = {
  {text = "lols"}
}
}
}
for k,v in pairs(table["someTable"]) do
print(v.text)
end