Arrays
Arrays are ordered collections for storing data. In Lua, all of the widely used programming containers (arrays, queues, sets, etc) are implemented by a single structure known as a table.
Arrays can hold any type of data, can be accessed and modified using the common indexing syntax ([]
), and can be cleared through the assignment of nil
.
Syntax
In Lua, arrays can be created with curly braces {}
. The convention for indexing across Lua libraries is to begin with 1
in lieu of 0
. By default, arrays designate the first item in an array as index 1
. However, arrays can be created to use any custom set of indices prescribed.
a = {}; -- A new empty arrayfor i = 1, 5 doa[i] = 0endprint(a[5]) -- This will return 0print(a[6]) -- This will return nil because the index is out of range
The array created by the for
loop above contains 5 items, all of which are 0
. Any of these values can be accessed by passing an index. However, if a value is passed out of the range of the array (1-5), the code will return nil
. This is because nil
refers to missing data.
Accessing Elements
To access an array element, the desired value is called by its index. For example, to reference a value from the following array:
a = {[1] = “🎁”, -- "Gift"[2] = “🍌”, -- "Banana"[3] = “🍎” -- "Apple"}
Here, there is an array of elements that range from the indexes 1 - 3. To print “🍌”, the value must be referenced through its assigned index, which in this case is 2.
a = {[1] = “🎁”, -- "Gift"[2] = “🍌”, -- "Banana"[3] = “🍎” -- "Apple"}print(a[2]) -- “🍌” [Banana]
This can be done for every value within an array. This also works with dictionaries, but instead the assigned key must be used to retrieve a value. A key is essentially an index with the only difference being that a key may be assigned as any type, while an index must be a number.
For example:
dictionary = {[“🍎”] = “Apple”,[“🍌”] = “Banana”,[“🍊”] = “Orange”}print(dictionary[“🍊”]) -- “Orange”
It is important to note that Lua will not print the key of the dictionary being used, but will instead print the value related to the requested key. This behavior also applies to arrays.
Replacing Elements
Values can also be reassigned by calling the relevant index and assigning the new value as demonstrated below.
a[2] = “🍊” -- Now the object at index is 2 is orange instead of banana
To remove an element, the index can be reassigned to nil
.
a[2] = 'nil'
Getting the Length of an Array
Often it is valuable to access the length or size of an array. In Lua, the #
symbol is prepended to the array to return the length.
Note: This syntax will return the last index, in some cases this may not be equivalent to the size of the array.
b = {2, 4, 6, 8}for i = 1, #b do -- This will cycle through and print each element of the arrayprint(i)end
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours