When an array A is inserted into an array B and one of the elements of array A is changed, the array that was inserted into array B changes to the new value of array A. Is this expected behaviour?
Code example
local varyingArray={1,2,3,4} --array that will be changed multiple times in the for loop
local arrayCollection={} --collection of arrays
for i=1, 4 do
varyingArray[4]=i --sets the element 4 of varyingArray to i
table.insert(arrayCollection, varyingArray) --stores varyingArray current value
end
for i, v in pairs(arrayCollection) do
print(v) --prints each array of arrayCollection
end
Output
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 4
}
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 4
}
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 4
}
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 4
}
What was supposed to be printed
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 1
}
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 2
}
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 3
}
▼ {
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 4
}
I can’t post bug reports due to rank requirements, so if this isn’t expected behavior, i would be grateful if anyone did a bug report for me. If this is supposed to happen, please inform be below.