Updated May 6, 2023
Definition of Lua Array
An array stores the elements in Lua; Array represents the ordered set of objects we store inside. The array can be one-dimensional or multi-dimensional in Lua. If we have created a one-dimensional array, it will contain several rows inside it; on the other hand, creating a multi-dimensional array will contain multiple columns and rows. Array in Lua uses an indexing table, the size of the Array in Lua is not fixed and can be changed or grown at runtime or based on the requirement. The primary usage of the Array is to store data, and we can perform operations on them one by one by iterating them. In the coming section of the tutorial, we will discuss the Array and their implementation in detail for a better understanding and their usage while programming.
Syntax:
As we discussed, an array in any programming language is used to store the elements; here in Lua, we do not need to specify the initial size of an array; it is growable. Let’s take a look at its syntax for better understanding; see below;
1) Initialize the array at the time of creation: Follow the below syntax if you want to initialize it at the time of creation only;
array_name = {"val 1", "val 2", "val 3" ,"so on .."}
2) Blank initialization while creation: Follow the below syntax if you want to initialize it after certain;
array = {}
We have two ways to create an array in Lua, which is straightforward. Let’s take a practice syntax for beginners to start with ;
e.g. :
array = {"abc", "xyz", "so on"}
In the coming section of the tutorial, we will discuss the internal working of the Array in Lua for a better understanding of its implementation.
How does Array work in Lua?
As we already know, Array stores ordered objects created using the indexing table. Also, we do not specify the size of the Array at the time of creation; it means they can be growable. We have two things while creating of Array in Lua; see below;
1) Initialize the array at the time of the creation
2) Blank initialization
In Lua, we have two types of Array which are mainly categorized as below;
1) single dimensional Array
2) Multi-dimensional Array
Let’s discuss each of them in detail with practice syntax to understand its implementation; see below;
1) Single dimensional Array: Single dimensional Array only contains a collection of rows. Single dimensional Array is straightforward to create and understand because it does not have a column associated with it. Rows can be in any number; there is no restriction for that. We can take an example to get more idea about single dimensional Array in Lua and how to create them;
e.g. :
a1 = {"abc", "xyz", "asd", "gft", "mkj", "ftr"}
for i = 0, 10 do
print(a1[i])
end
In the above lines of code, we are trying to create a single-dimensional array in Lua; as you can see, we have created an ‘a1’ Array and assigned some values inside it. Suppose you want to know the array elements that we may need to iterate them using for loop in Lua. We can mention the range and print the values. So this will only contain the rows or a collection of rows representing an array’s elements.
2) Multi-dimensional Array: Multi-dimensional Array is a collection of rows and columns; they are not as straightforward as a single-dimensional array; it is a bit different. Creating a multi-dimensional array in Lua, we have two different ways to do,
- a) we can use arrays of arrays
- b) single dimensional Array with manipulation indices (as per Lua’s documentation.)
Let’s take a look at its syntax to understand its implementation in detail;
e.g. :
Steps to create multi-dimensional in Lua :
1) First, we will create the Array without size initialization.
2) After that, we can create a new row inside it, with dimensions as N by M. This is the easy way to create it per the Lua documentation on their official site.
array[i] = {}
array[i][j] = {}
Some points which need to be kept in mind while working with an array in Lua are below;
1) Indexing in arrays in Lua always starts with 1. By default, it begins with 1, but we can also modify it to create negative values.
2) If we try to access the element not present at the index, it will return nil as output.
3) Their multi-dimensional array implementation differs from another programming language; it is not straightforward.
Examples
1) In this example, we are creating two arrays of string and integer and assign them several values. Followed by the for loop to print the values inside the arrays. This is a sample and simple example for beginners to implement and start using Array in Lua programming.
Example:
print("Demo to show array in lua !!")
a1 = {"value 1", "value 2", "value 3", "value 4", "value 5", "value 6"}
a2 = {100, 200, 300, 400, 500, 600}
a3 = {1.2, 3.3, 4.1, 3.4, 6.7}
a4 = {"amit", "anjali", "arun", "anshika"}
a5 = {'A', 'B', 'C', 'D', 'E', 'F'}
print("Print the result of array one::")
for i = 0, 7 do
print(a1[i])
end
print("Print the result of array two::")
for i = 0, 7 do
print(a2[i])
end
print("Print the result of array three::")
for i = 0, 7 do
print(a3[i])
end
print("Print the result of array four::")
for i = 0, 7 do
print(a4[i])
end
print("Print the result of array five::")
for i = 0, 7 do
print(a5[i])
end
Output:
Conclusion
By using an array, we can store our data, print and manipulate it. To iterate the array objects, we can use for loop, which is easy to use and handle. Also, the Array in Lua implementation is very clean and clear for developers to understand and implement.
Recommended Articles
We hope that this EDUCBA information on “Lua Array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.