Lua : add a table in a table
Question
I recently started learning Lua and I have a question about tables :
local mytable = {10,11,12}
I would add another table in mytable[3]
, if I do this :
table.insert(mytable[3], {[15] = {}})
Will it works ?
And if it works, what will be the "final" result ?
mytable[3][15] = {}
or
mytable[3][1][15] = {}
?
It's probably a basic question but it will help me :p
Solution
The first argument of table.insert
should be the table being inserted, so the correct syntax is:
table.insert(mytable, {[15] = {}})
After this, the value of mytable[4]
(note that Lua table index starts from 1
) is the table {[15] = {}}
, and the value of mytable[4][15]
is therefore an empty table.
To make mytable[3]
a table {[15] = {}}
, use assignment instead:
mytable[3] = {[15] = {}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow