Concepts - Tables and Arrays
What Are Tables?
Tables are variables that allow you to store multiple values within them, like a container. Tables provide you with the ability to map values to specific keys, like how a dictionary maps definitions (the values) to specific words (the keys).
Going off of the dictionary example, one can look up any word in any dictionary and get the definition of it in return, in the same way that one can look up a key in a table and retrieve the value stored at, or on, that key.
Keep in mind that this is an extremely basic explanation, and there really is so much more to tables than what is described here.
Keys
As mentioned above, tables in Lua contain keys and values; keys are identifiers for values, and are known as the "index" of any entry. Entries in a table might also be called "key/value pairs."
One could use keys in multiple ways. First, keys can be numeric and/or represent the order of things, i.e.:
Keys can also be strings, such as:
When used to represent the order of things, keys let a table function more like list
, or even array
, objects in other languages - the keys are numeric, and sequential (in order). When strings (or other types) are used as keys, tables can be used more like a dictionary
or hash map object. The best part of all this is that tables in Lua are able to use both of these kinds of "modes" at the same time!
It's not just numbers or strings that can be used as keys, however. According to the official Lua Users' Wiki, keys can be any type of variable, except for nil
. More often than not, however, they are usually defined as either numbers or strings.
There can only be one value for every key in a table - keys must be unique. The only way to store more than one value on a specific key is to use a new table as the value. (This is still technically only one value, though - the table counts as a single value, but can have other stuff stored inside of it. This table-inside-a-table scenario is called a "nested" table.)
Values
Values can be any data type - even other tables!
Table creation example
Looping over a table
There are a few ways to loop over all of the key/value pairs in a table. The method you use will depend on how the keys are arranged.
If Lua filled in your numeric "order" keys automatically, like in the last example, you can use a for loop to loop through the table just fine.
If there are any keys that were NOT defined as numeric "order" keys, i.e. string keys, OR if you defined a numeric key "out of order," you will have to use pairs()
to loop over the table.
pairs
will loop over all of the values in the table, including the numeric/order keys AND the string (or other type) keys; in other languages, this is kind of like a for each
loop.
Usage of the table library
The table library is a standard Lua library which provides functions to manipulate tables. In Garry's Mod there are several extra useful functions added to this library. These are some examples of functions defined in the table
library.
table.Merge
table.Add
Other table functions
You can refer to the library page for the table
library to discover all of the table
library's functions.
Other functions
Some other functions are also useful for tables, including, but not limited to:
ipairs
We went over the use of pairs
in the Looping over a table section. pairs
has a sister function called ipairs
that does the exact same thing pairs
does (loops over all the k/v pairs in a table), but for the numeric keys only.
RandomPairs
RandomPairs
loops through a table, but in a random order.
SortedPairs
SortedPairs
lets you loop over all the key/value pairs in table, but it sorts the keys alphabetically. This is most useful for tables with string keys.