Tables


Tables are a very important and unique feature of Lua. They are series associative arrays of data, functions, objects and even other tables. They are created by enclosing them with curly brackets { }. By "associative array" we mean that every data is composed of a key/value pair.

> t = {}
> t["anything"] = 33
> print (t["anything"])
33
> t[12] = "something"
> print (t[12])
something
>

Any value can be used as key, but strings are so common that there is a shortcut for it: <table> . <key> (table-dot-key). Note: in this case, the string that makes the key can't start with a number.

> t.foo = 12
> = t.foo
12
> = t["foo"]
12
> = t[foo] -- foo here is not a string, it's a variable
nil

You can build tables separating key/value pairs with a comma within the { }:

> myTable= {cat = 33, dog = "ugly", [33] = "fly"}

> =myTable.cat
33

> =myTable.dog
ugly

> =myTable.33
stdin:1: '<eof>' expected near '.33'   -- key starts with a number! Can't use this shortcut

> =myTable[33]                         -- have to use the regular way
fly

You don't copy a table by assigning it to another variable. Both variables will be pointers to the same table. If you need an independent copy, you must do it "manually".

for...in pairs...do

The for loop in lua has a special construct to deal with tables. That is the for...in pairs...do. It is a for loop that take one key/value pair for each iteration:

> myTable= {cat = 7, dog = 12, bird = 3}
> for animal , age in pairs(myTable) do print(animal, age) end
bird    3
dog    12
cat    7

Arrays:

You can create arrays by listing the values, separated by commas, inside { }, without keys:

> myTable= {"dog", "cat", 33}
> =myTable[1]
dog
> =myTable[2]
cat
> =myTable[3]
33

Notice that the first index is 1 , not 0 (zero) like many other languages.

You may, however mix values with and without keys:

> t = {"bat", "owl", [123]="cat", "ant", name="dog", "fly"}
> for k,v in pairs(t) do print(k,v) end
1    bat
2    owl
3    ant
4    fly
123    cat
name    dog
for...in ipairs...do

To loop over an array use ipairs instead of pairs .It only gives you the consecutive integer keys from 1. It guarantees their order sequence. Withpairsthe number keys will not necessarily be given in the correct order!

> t = {"bat", "owl", [123]="cat", "ant", name="dog", "fly"}
> for k,v in ipairs(t) do print(k,v) end
1    bat
2    owl
3    ant
4    fly
Lenght of an array

You use the operator #. Be careful, it does not count the items, just returns the last integer key.

> t = {"fly", "dog", "elk"}
> =#t
3

t = {"bat", "owl", [123]="cat", "ant", name="dog", "fly"}
> =#t
4
Add an item to an array

table.insert() inserts items into an array:

> t = {"fly", "dog", "elk"}
> table.insert(t, "cow")
> for k,v in ipairs(t) do print(k,v) end
1    fly
2    dog
3    elk
4    cow

An optional argument gives the index where the item should be inserted (not replaced):

> t = {"fly", "dog", "elk"}
> table.insert(t,2, "HOUSE")
> for k,v in ipairs(t) do print(k,v) end
1    fly
2    HOUSE
3    dog
4    elk
Removing items from an array

table.remove() works in the same way as table.insert(), but removing items. If the second argument is not given, it removes the last item of the table:

> t = {"fly", "dog", "elk"}
> table.remove(t)
> for k,v in ipairs(t) do print(k,v) end
1    fly
2    dog

The second argument selects the item to be removed:

> t = {"fly", "dog", "elk"}
> table.remove(t,2)
> for k,v in ipairs(t) do print(k,v) end
1    fly
2    elk
Creating a string from array items

table.concat() returns a string made with the elements of the array:

> t = {"My ", "house ", "is ", "red"}
> = table.concat(t)
My house is red

A second argument gives a separator:

> t = {"My ", "house ", "is ", "red"}
> = table.concat(t,";")
My ;house ;is ;red
Sorting an array

table.sort() sorts the elements of an array:

> t = {"c", "d", "a", "z"}
> table.sort(t)
> for k , v in pairs(t) do print(k, v) end
1    a
2    c
3    d
4    z

results matching ""

    No results matching ""