Lua Tables w/ Changing Variables - Mental Block

Posted by bottles on Stack Overflow See other posts from Stack Overflow or by bottles
Published on 2011-11-27T09:07:53Z Indexed on 2011/11/27 9:51 UTC
Read the original article Hit count: 167

Filed under:

I always assumed that changing the value of k from "x" to 20 would eliminate "x". So why then in this example are we able to go back and reference "x"?

a = {}
k = "x"
a[k] = 10
print(a[k])        ---> Returns 10
print(a["x"])      ---> Returns 10
a[20] = "great"
k = 20
print(a[k])        ---> "great"
a["x"] = a["x"] + 1
print(a["x"])    --> 11

Why does that last print command work, and return 11? I thought we set k = 20. Why is "x" even in the picture?

© Stack Overflow or respective owner

Related posts about lua