A table is similar to a list—a group of related numbers, strings, objects, or even other tables. Tables can be very useful when doing intermediate or advanced scripting.

Creating a table

Curly brackets are used to create tables:

local t = {"apple", "banana", "peach", "papaya", "pear"}

Getting data from a table

To get data from a table, use square brackets.

fruit = t[1]
 print(fruit) --prints "apple"
 anotherfruit = t[4]
 print(anotherfruit) --prints "papaya"


You can also use tables as simple key/value pairs. The creation is similar to a normal table (array).

local t = {apple="green", orange="orange", banana="yellow"}

Getting information is similar; you use the key name instead of a number index.

color = t[banana]
 print(color) -- prints "yellow"

See also

Community content is available under CC-BY-SA unless otherwise noted.