Skip to content

Tables

Warning

Indexes in Lua start from 1! Keep that in mind when accessing array elements by index.

Lua tables can have different behaviors. They can work as arrays, dictionaries, classes, etc.

Arrays

One of the 2 basic behaviors of tables is defined like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
table = {
    'element 1',
    'element 2',
    123,
    true
    false,
    "Another element",
    {
        'this can be',
        'element too'
    }
}

Accessing elements:

1
val = table[6] -- equals to "Another element"

Dictionaries

The second basic behavior of tables is defined like below:

1
2
3
4
5
table = {
    element_1 = 123,
    [52] = "52th element",
    ["Some thing"] = "thing Some"
}

You can access elements like this:

1
2
3
4
val = table.element_1       -- equals to 123
val = table[52]             -- equals to "52th element"
val = table["Some thing"]   -- equals to "thing Some"
val = table["element_1"]    -- equals to 123

More advanced stuff will be explained in the Part 2.