Corona SDK Mobile Game Development:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Tables

Tables are the proprietary data structure in Lua. They represent arrays, lists, sets, records, graphs, an so on. A table in Lua is similar to an associative array. Associative arrays can be indexed with values of any type, not just numbers. Tables implement all these structures efficiently. For example, arrays can be implemented by indexing tables with integers. Arrays do not have a fixed size, but grow as needed. When initializing an array, its size is defined indirectly.

The following is an example of how tables can be constructed:

1 a = {}    -- create a table with reference to "a"
2 b = "y"
3 a[b] = 10    -- new entry, with key="b" and value=10
4 a[20] = "Monday"  -- new entry, with key=20 and value="Monday"
5 print(a["y"])    -- 10
6 b = 20
7 print(a[b])     -- "Monday"
8 c = "hello"     -- new value assigned to "hello" property
9 print( c )    -- "hello"

You will notice in line 5 that a["y"] is indexing the value from line 3. In line 7, a[b] uses a new value of variable b and indexes the value of 20 to the string, "Monday". The last line, c is different from the previous variables and its only value is the string, "hello".

Passing a table as an array

Keys of a table can be consecutive integers, starting at 1. They can be made into an array (or a list).

colors =  {
[1] = "Green", 
[2] = "Blue", 
[3] = "Yellow", 
[4] ="Orange", 
[5] = "Red"
}
print(colors[4]) -- Orange

Another way of writing table constructors to build arrays in a faster and convenient way that doesn't require writing out each integer key is as follows:

colors = {"Green", "Blue", "Yellow", "Orange", "Red"}
print(colors[4]) -- Orange

Altering contents in a table

While working with tables, you can modify or remove the values already in it, and also add new values to it. This can be accomplished using the assignment statement. The following example creates a table with three people and their favorite type of drink. You can make an assignment to change one person's drink, add a new person-drink pair to the table, and remove an existing person-drink pair.

drinks = {Jim = "orange juice", Matt = "soda", Jackie = "milk"}
drinks.Jackie = "lemonade" -- A change.
drinks.Anne = "water" -- An addition.
drinks.Jim = nil -- A removal.
print(drinks.Jackie, drinks.Anne, drinks.Matt, drinks.Jim)
-- lemonade water soda nil

drinks.Jackie = "lemonade" overwrites the original value of drinks.Jackie = "milk".

drinks.Anne = "water" adds a new key and value to the table. The value of drinks.Anne before this line would have been nil.

drinks.Matt = "soda" stays the same since there were no alterations to it.

drinks.Jim = nil overwrites the original value of drinks.Jim = "orange juice" with nil. It removes the key Jim from the table.

Populating a table

Ways to populate a table is to start with an empty table and add things to it one at a time. We'll be using constructors, which are expressions that create and initialize tables. The simplest constructor is the empty constructor, {}.

myNumbers = {} -- Empty table constructor

for i = 1, 5 do
  myNumbers[i] = i 
end

for i = 1, 5 do
print("This is number " .. myNumbers[i])
end

The following is the result from the terminal:

--This is number 1
--This is number 2
--This is number 3
--This is number 4
--This is number 5

The preceding example shows that myNumbers = {} is an empty table constructor. A for loop is created and calls myNumbers[i] five times starting from the number 1. Each time it is called, it is incremented by 1 and then printed out.