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).
Following 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.
Sequential tables, arrays, lists
A sequential table is a table where the keys are numbers, starting with 1
, ever increasing and without gaps. This is known as an "Array" or a "List" in other programming languages. In Lua, there's no such distinction.
Keys
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. 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 - they 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.
If there are any keys that were NOT defined with numeric keys, i.e. string keys, or if you defined numeric key(s) "out of order" or with gaps, you will have to use pairs to loop over the entire 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.
ipairs
pairs has a sister built-in function called ipairs that does the exact same thing pairs does (loops over all the k/v pairs in a table), but for sequential tables only.
RandomPairs
RandomPairs is a Garry's Mod specific function that loops through a table, but in a random order.
SortedPairs
SortedPairs is another Garry's Mod specific function that 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.
Similarly SortedPairsByValue can be used to loop over a table, but sorted by the value instead of the key.
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.