I'm making some kind of game and I've been wondering if you can store a table inside a table? Example:
table1 = {
"String";
134;
}
table2 = {
table1;
"String2";
1234;
}
I'm making some kind of game and I've been wondering if you can store a table inside a table? Example:
table1 = {
"String";
134;
}
table2 = {
table1;
"String2";
1234;
}
But you can do more than point to a table. Tables can store any value, and tables are values, so we can just do this:
local t = {
{1, 2, 3},
{"hi", "hola", "bonjour"}
}
print( t[2][3] ) --> bonjour
Although it's generally best to use dictionaries in this case so we can choose our own key.
local t = {
numbers = {1, 2, 3},
greetings = {"hi", "hola", "bonjour"}
}
print( t.greetings[3] ) --> bonjour
So yes, you can, quite literally, have a table within a table.
And you can have a table withing a table within a table within another table, all the way to infinity!
local t = { { { { { {"hi"} } } } } }
print( t[1][1][1][1][1][1] ) --> hi
but yeah don't do that, it's pretty pointless.
Yes, no
You can store a pointer to the table inside of your table.
In your code, when you access table2[1], it will point back to table1. table1 isn't technically 'in' table2, and there is no such thing as Parent inside of a table (The table doesn't know where it is linked to).
TL;DR - Yes, you can.
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
Wow. Thanks!