This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Gii2000's profile picture

[MC 1.9.8][CC 1.78][SSP] Writing to matrix isn't correct

Started by Gii2000, 23 February 2016 - 04:06 AM
Gii2000 #1
Posted 23 February 2016 - 05:06 AM
The bug can be seen with this sample program.

local table1 = {" ",1}
local table2 = {}
for a = 1, 2 do
   table2[a] = table1
end
local table3 = {}
for a = 1, 2 do
   table3[a] = table2
end
table3[1][1][1] = "H"
print(table3[2][1][1]) -- prints "H"
print(table3[1][2][1]) -- prints "H"
print(table3[2][2][1]) -- prints "H"
I don't know if this bug was discovered before, but I couldn't find anything about it in the bugs forums.

I'm thinking that inserting a table into another for a specific number of time (2 for demonstration,) and putting that into another table twice, would create a matrix that I can modify a single piece of. And obviously something is wrong since four H's are in the table. So I think this is a bug, but it might just be me doing something wrongly.

P.S. - Using table.insert() instead of table[a] has the same effect
Lyqyd #2
Posted 23 February 2016 - 05:13 AM
Moved to Ask a Pro.

This is not a bug.
Bomb Bloke #3
Posted 23 February 2016 - 05:16 AM
The reason why is that when you do:

local table1 = {" ",1}

… you're building a table and assigning a pointer leading to it to "table1".

So when you do:

for a = 1, 2 do
   table2[a] = table1
end

… you're making copies of that pointer, and not the table itself.
Edited on 23 February 2016 - 04:18 AM