Saving Tables inside Tables in Lua
Posted: Mon Jan 06, 2020 8:41 am
Hi,
Can you help me understanding something?
If i run the code
it outputs {1, 3}, as i expected.
But if i run the code
then i expected result == {{1,2},{1,2,3}}
but i got result == {{1,2,3},{1,2,3}}
(I know comparing two tables and the values of two tables are two different things in lua, it's just the shortest way to write it down here. I checked by printing the values and not by comparing.)
Where is my thinking mistake?
Can you help me understanding something?
If i run the code
Code: Select all
tab = {1,3}
result = {tab}
tab = {1,4}
return result
But if i run the code
Code: Select all
local x = 3
local tab = {}
local result = {}
for k = 2, x do
for v = 1, k do
tab[v] = v
end
result[#result+1] = tab
end
but i got result == {{1,2,3},{1,2,3}}
(I know comparing two tables and the values of two tables are two different things in lua, it's just the shortest way to write it down here. I checked by printing the values and not by comparing.)
Where is my thinking mistake?