Ank
Ank

Reputation: 6270

How Lua tables work

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.)?

Upvotes: 1

Views: 3209

Answers (3)

Prehistoricman
Prehistoricman

Reputation: 121

There's another way of creating keys in in-line table declarations.

x = {["1st key has spaces!"] = 1}

The advantage here is that you can have keys with spaces and any extended ASCII character. In fact, a key can be literally anything, even an instanced object.

function Example()
    --example function
end
x = {[Example] = "A function."}

Any variable or value or data can go into the square brackets to work as a key. The same goes with the value. Practically, this can replace features like the in keyword in python, as you can index the table by values to check if they are there.

Getting a value at an undefined part of the table will not cause an error. It will just give you nil. The same goes for using undefined variables.

Upvotes: 2

flameleo11
flameleo11

Reputation: 1

local w = {
    --[1] = "another field"; -- will be set this value
    --["1"] = nil;  -- not save to this place, different with some other language
    x = 0;
    y = 0;
    label = "console";

}
local x = {
    math.sin(0);
    math.sin(1);
    math.sin(2);
}

w[1] = "another field" -- 
x.f  = w

print (w["x"])

-- because x.f = w
-- x.f and w point one talbe address
-- so value of (x.f)[1] and w[1] and x.f[1] is equal
print (w[1])
print ((x.f)[1])
print (x.f[1])

-- print (x.f)[1] this not follows lua syntax 
-- only a function's has one param and type of is a string
-- you can use print "xxxx"
-- so you print x.f[1] will occuur error


-- in table you can use any lua internal type 's value to be a key
-- just like

local t_key = {v=123}
local f_key = function () print("f123") end

local t = {}
t[t_key] = 1
t[f_key] = 2

-- then t' key actualy like use t_key/f_key 's handle 

-- when you user t[{}] = 123, 
-- value 123 related to this no name table {} 's handle

Upvotes: 0

Max E.
Max E.

Reputation: 1917

You have to realize that this:

t = {3, 4, "eggplant"}

is the same as this:

t = {}
t[1] = 3
t[2] = 4
t[3] = "eggplant"

And that this:

t = {x = 0, y = 2}

is the same as this:

t = {}
t["x"] = 0
t["y"] = 2

Or this:

t = {}
t.x = 0
t.y = 2

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 x which is separate from w.

d.) places a reference to w inside of x. (NOTE: It does not actually make a copy of w, 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.f which is just another way to refer to w because of line d.). Then we look up the first element of that table, which is "another field" because of line c.)

Upvotes: 13

Related Questions