insert - Inserting a changing table into another table. Tables values are the same when they should be different -
i trying make table tables inside of it. tables go inside change previous value. end result tables inside main table equal each other , equal latest value.
local array = {} local x local y function test(a) if a==1 x = {1,1} print("x returned") y = x k,v in pairs(x) print(k,v) end return x end if a>=1 p=math.random(1,2) n=math.random(2,4) table.remove(y,p) table.insert(y,p,n) print("") print("y returned") k,v in pairs(y) print(k,v) end return y end end array[1] = test(1) array[2] = test(2) array[3] = test(3) print("") k,v in pairs(array) print(k,v) end testtable=array[1] print("") k,v in pairs(testtable) print(k,v) end
output:
x returned 1 1 2 1 y returned 1 1 2 3 y returned 1 1 2 4 1 table: 0x678300 2 table: 0x678300 3 table: 0x678300 1 1 2 4
the 3 tables inside array
should different each other. doing wrong? there tables don't know?
tables objects, means variable doesn't contain value of table. instead, holds reference table. in function test
, when do:
y = x
now y
, x
both reference same table. that's why 3 tables inside array
same.
Comments
Post a Comment