Lua: odd table copying behavior
I’m really a novice to Lua and programming, so I guess the following is just a “pilot’s error”.
I want to create a function to “rotate” values in a table, so that I can turn {1, 2, 3, 4} into {4, 1, 2, 3}. I guess there are several ways to accomplish this, but the one I chose involves creating a copy (called copy, surprisingly) of the table original whose content I want to rotate, and then run this code:
for i = 1, #original do
if i==1 then
original[i]=copy[#original]
else
original[i]=copy[i-1]
end
When I do the table copy via this code,
for i = 1, #original do
copy[i] = original[i]
end
everything works as expected, and {1, 2, 3, 4} becomes {4, 1, 2, 3}.
But when I use this shorter bit of code to do the copy action,
copy = original
all of a sudden {1, 2, 3, 4} becomes {4, 4, 4, 4}, which is unusable. At first I thought that the shorter copy action itself is wrong, but interestingly, immediately after this copy action both copy and original contain the same values at the same positions.
Please, can somebody be so kind to explain what is causing this odd behavior?