Mojimi
Mojimi

Reputation: 3151

table.remove in lua acting weirdly

tbl1 = {1}

tbl2 = tbl1

table.remove(tbl2,1)

print(tbl1[1])
-- >> nill

The above example is a simplification of the problem in my code, by removing a index from tbl2, it also removes from tbl1, is there a reason for this to be happening?

Upvotes: 2

Views: 121

Answers (1)

Oliver
Oliver

Reputation: 29463

Variables in Lua are references to objects, and so a=b sets the variable named a to refer to the object that b refers to. If b is a table, then after the assignment both a and b point to the same table object.

Upvotes: 3

Related Questions