Definition of Lua Array
An array is used to store the elements in Lua, array represents the ordered set of an object that we store inside it. Array can be one dimensional or multi-dimensional in Lua. If we have created a one-dimensional array then it will contain several rows inside it, on the other hand on the creation of a multi-dimensional array will contain multiple columns and rows. Array in Lua uses indexing table, size of the array in Lua is not fixed can be changed or grow at runtime or based on the requirement. Basic usage of the array is to store data and also we can perform operations of them one by one by iterating them. In the coming section of the tutorial, we will discuss more the array and their implementation in detail for 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 in nature. Let’s take a look at its syntax for better understanding see below;
1) Initialize 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 here to create an array in Lua, which very easy and clear to use. 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 Array works in Lua?
As we already know that array is used to store objects which is ordered in nature, Lua array is created using the indexing table, also we not specify the size of the array at the time of creation it means they can be growable in nature. We have two things while creation of array in Lua see below;
1) Initialize array at the time of the creation
2) Blank initialization
In Lua we have two types of the 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 contains a collection of rows only. Single dimensional array is very easy to create and understand because it does not have a column associate with it. Rows can be in any number there is no restriction for that. we can take on example to get more idea about single dimensional array in Lua how to crate 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 ‘a1’ array and assign some values inside it. If you want to see 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 collection of rows which represent the elements of an array.
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. To create a multi-dimensional array in Lua we have two different ways to do this,
- a) we can use arrays of arrays
- b) single dimensional array with manipulation indices (this is as per the documentation of Lua.)
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 as 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 see below;
1) Indexing in arrays in Lua always starts with 1. By default, it starts with 1, but we can modify it to starts with negative values as well.
2) If we are trying to access the element which is not present at the index then it will return us nil as output.
3) There implementation of a multi-dimensional array is very different from another programming language, it is not that 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 them and manipulate them as well. To iterate the array objects we can use for loop which is easy to use and handle. Also, the implementation of the array in Lua is very clean and clear for developers to understand and implement.
Recommended Articles
This is a guide to Lua Array. Here we discuss the definition, syntax, types of array, How array works in Lua? and example with code implementation. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses