what is the difference between the variable assignment
local newpos = {}
newpos.x = 1 ----- or --------- newpos[x] = 1
i know not what i speak but to me these seem to be the same thing if not similar?
newpos.x = 1
is the same as newpos["x"] = 1
that is they both set the value stored at key string "x"
to 1
.
newpos[x] = 1
is different. This is set the value stored at key contents of variable x
to 1
.
Try it and see.
local newpos = {}
newpos.x = 1
print(newpos.x, newpos["x"], x, newpos[x])
newpos["x"] = 2
print(newpos.x, newpos["x"], x, newpos[x])
local x = "var"
print(newpos.x, newpos["x"], x, newpos[x])
newpos[x] = 3
print(newpos.x, newpos["x"], x, newpos[x])
Results for the above:
1 1 nil nil
2 2 nil nil
2 2 var nil
2 2 var 3
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments