I am starting to learn Lua from Programming in Lua (2nd edition)
I didn’t understand the following in the book. Its very vaguely explained.
a.) w={x=0,y=0,label="console"}
b.) x={math.sin(0),math.sin(1),math.sin(2)}
c.) w[1]="another field"
d.) x.f=w
e.) print (w["x"])
f.) print (w[1])
g.) print x.f[1]
When I do print(w[1]) after a.), why doesn’t it print x=0
What does c.) do?
What is the difference between e.) and print (w.x)?
What is the role of b.) and g.)?
You have to realize that this:
is the same as this:
And that this:
is the same as this:
Or this:
In Lua, tables are not just lists, they are associative arrays.
When you print
w[1], then what really matters is line c.) In fact,w[1]is not defined at all until line c.).There is no difference between e.) and
print (w.x).b.) creates a new table named
xwhich is separate fromw.d.) places a reference to
winside ofx. (NOTE: It does not actually make a copy ofw, just a reference. If you’ve ever worked with pointers, it’s similar.)g.) Can be broken up in two parts. First we get
x.fwhich is just another way to refer towbecause of line d.). Then we look up the first element of that table, which is"another field"because of line c.)