Introduction to tables
Tables are the only data structure available in Lua. The table data structure is powerful enough to implement other data structures. Tables can also be used to extend the Lua language with a class system, or even a mixin system, which is an alternative to class-based composition. So, what is a table?
Tables are basically a dictionary or array. A table is a key-value pair. If the keys to the table are numeric, the table represents an array. If the keys are non-numeric or mixed, the table is a dictionary. Anything can be used as a key in a table, other than nil
. Anything, including nil
, can be a value.
Creating tables
A table in Lua is created with the curly brace {}
symbols. After a table is created, it needs to be assigned to a variable. If you don't assign the table to a variable, you won't be able to refer to it. The following code creates a new table and assigns it to the tbl
variable. The code then prints out the type of thetbl
variable, which should be a table:
tbl...