Lua Arrays
In this article, we will discuss what Lua arrays are, how to create them, and how to use them effectively.
Lua Arrays are collections of elements, usually of the same type, stored in contiguous memory locations and accessed using indexes and subscripts.
The use of arrays provides a convenient way to store and manipulate large amounts of structured data, whether they are fixed or dynamic.
Lua arrays are represented by tables and are implemented as tables with integer keys beginning with 1. The values can be of any data type, including nil.
Lua Array Creation
To create an array in Lua, you can initialize a table with values. Lua arrays are dynamic, meaning you can add or remove elements at any time.
An example of array initialization is provided below where an array ‘arr’ is initialized:
arr = {"Dell", "Lenovo", "HP", "Apple"}
Accessing Elements In An Array
In Lua, you can access array elements using the square bracket notation [].
Square brackets enclose the index of the element you want to access. In Lua, array indexes start at 1, not 0.
This code, for example, will allow us to access the second element of the arr array:
Example: 
Add Element To Array
Lua allows you to add new elements to arrays by assigning values to new indexes.
To accommodate the new element, Lua automatically increases the array size.
For example, to add a new element with the value “Acer” to the arr array, we can use the following code:
arr[5] = "Acer"
Let’s use the above code snippet in an example:
Example: 
Iterate Over Array
Example: 
Multidimensional Arrays
Up until now, we have seen arrays that are one-dimensional.
By using tables within tables, you can also create multidimensional arrays in Lua.
This is the syntax to creates an array with two rows and three columns in Lua:
arr = { {2, 4, 6}, {8, 10, 12}, }
Following example prints the contents of the array defined above: