# Tables

Oct 02 2018, 2:23 AM PST 5 min

A table is a data type in Lua that is useful to store multiple values, including numbers, strings, functions and more. They are called tables because they act like a grid with two columns:

Key Value
A key A value

The key column is used to find a row in the table, and the value is the value that is stored in that row. Both the key and value can be any Lua value such as a number, string, an `Instance` such as a `Part` and even other tables. However, neither the key or value can be nil. Lua tables can have a combination of numbers, strings, `Instance|Instances` and tables as keys.

Another way to describe it is that by inputting the key, you receive the value.

### Arrays

An array is a list of values, stored in order. It is a table where the keys are sequential integers starting at 1, e.g. 1, 2, 3, 4. Arrays are useful for creating ordered lists of things, such as a list of players with special permissions.

#### Creating arrays

Arrays are created with a pair of braces ("{" and “}”), containing the values to store in the array separated by commas (",") or semicolons (";"). The values can be of any type.

``````local myArray = {"A string", 3.14159, Workspace.Part}
local myEmptyArray = {}
``````

#### Reading from and writing to arrays

To read from an array, add a pair of brackets ([ and ]) after the array, and put the number of the element you want inside it. The element in the array start at index 1.

``````print(myArray) --> A string
print(myArray) --> 3.14159
print(myArray:GetFullName()) --> Workspace.Part

myArray = "Pi"
print(myArray) --> Pi
``````

#### Iterating over arrays

You should use a numeric `for` loop to iterate over an array.

``````for index = 1, #myArray do
local value = myArray[index]
print(index, " = ", value)
end
``````

#### More information

You can get the length of the array with the # operator:

``````print(#myArray)       --> 3
print(#myEmptyArray ) --> 0
``````

Lua’s table manipulation functions allow you to easily do things such as add and remove values from an array. For more information see the page on the table Lua library.

### Dictionaries

Dictionaries are an extension of arrays. While an array stores an ordered list of items, a dictionary stores a set of key/value pairs. For example, in a real dictionary, the “keys” are the words, and the “values” the definition.

#### Creating a dictionary

Once again, dictionaries are created with braces

``````local myDictionary = {
["Roblox"] = "A massively multiplayer online game",
["Wiki"] = "A Web site developed collaboratively by a community of users",
["Lua"] = "A lightweight multi-paradigm programming language"
}
``````

Like arrays, dictionaries are not restricted to strings. The values can be any type, but the keys are restricted to non-nil values.
For example, this would be a valid dictionary:

``````local playerScores = {
[game.Players.Telamon] = "Over 9000!",
[game.Players.Roblox] = 1337,
[game.Players.Sorcus] = Enum.DialogTone.Enemy
}
``````

And this wouldn’t:

``````local playerScores {
[nil] = "Hey!",
[game.Players.Telamon] = "Over 9000!",
[game.Players.Roblox] = 1337,
[game.Players.Sorcus] = Enum.DialogTone.Enemy
}
``````
##### Shorthand for string keys

If a key in a dictionary is a string, and a valid Lua identifier (that is, it can be used as the name of the `Articles/Variables|variable`), the quotes and brackets can be omitted:

``````local myDictionary = {
Roblox = "A massively multiplayer online game",
Lua = "A lightweight multi-paradigm programming language"
}
``````

#### Indexing a dictionary

Getting values into and out of a table is called indexing. An index in a table is like a row in the table model above. To index something in a table, you first need the key for the index you want to get or change the value in. You put the key in square brackets ([]) after the table that that is to be looked in. For example, to get or change the index with the key 1 in the table myTable, we write `myTable`. You can then use this exactly like a `Articles/Variables|variable` or value: it can be used or set to a different value. For example, to store the string “A value” in myTable in the row with the key 1, we would write:

``````myTable = {}
myTable = "A value"
``````

`myTable` can now be used in other places. For example, the print function:

``````print(myTable) -->A value
``````

If there is already an index in the table with the key that is being set, the old value will be replaced with the new one:

``````myTable = "A new value"
print(myTable) --> A new value (as apposed to "A value")
``````

If there isn’t a row with the key we’re using when we try to get a value, it will return nil:

``````print(myTable) --> nil
``````

#### Iteration

Iteration is the repetition of an action. In this case, you are iterating through the values in a table by taking each value and doing something with it. For-loops (and sometimes while-loops) are used to iterate with tables. You can use the `pairs` function to write a for-loop that goes through every key and value in a table.

``````for key, value in pairs(myOtherTable) do
print(key, "=", value)
end
``````

### Pass by reference

An important thing to understand when setting more than one variable to the same table is that tables are passed by reference. This means that the variable doesn’t directly contain the table itself, but that it holds a reference (or pointer) to it. This means that when more than one variable is set to a table, the variables do not each have a copy of the table, they refer to the same table, so any changes will be noticed by both variables:

``````var1 = {}
var2 = var1
var2["key"] = "value"
print(var1["key"]) -- prints "value" because var2 had pointed to var1's value (the table)
``````

Tags:
• coding
• concept
• lua