Updated April 10, 2023
Introduction to Lua array length
Whenever there is a need to arrange the objects in an order, then we make use of a data structure called array in Lua and the implementation of arrays can be done by creating indexes to the tables using integers and the length or size of an array is the number of elements stored in an array and array length or size is not fixed, it can change as per our requirements and in order to be able to determine the length of a given array we make use of the operator called # along with array name and it returns the size of the array. The indexes to the table generally starts from one but it is possible to create the indexes starting from zero or lesser than zero as per our requirement.
Syntax to determine array length in Lua:
#(array_name)
Where,
- array_name is the name of the array whose length is to be found.
- # is the operator that returns the length of the array.
Examples of Lua array length
Given below are the examples of Lua array length:
Example #1
Lua program to create an array consisting of elements and make use of # operator to determine the length of the array and display the elements of the array using the length of the array in a for loop.
Code:
--defining an array called newarray
newarray = {"Welcome", "to", "EDUCBA"}
--determining the length of the array using # operator and displaying it as the output on the screen
arraylength = #(newarray)
print("The number of elements stored in the array is:\t", arraylength)
--displaying the elements stored in the array using for loop and length of the array
print("\nThe elements stored in the array are:\n")
for a = 1, arraylength do
print (newarray[a])
end
Output:
Explanation:
- In the above program, we are creating an array called newarray and storing elements inside the array. Then we are determining the length of the array using # operator and storing it in a variable called arraylength.
- Then we are displaying the length of the array as the output on the screen. Then we are making use of for loop and length of the array to display the elements of the array as the output on the screen. The output is shown in the snapshot above.
Example #2
Lua program to create an array consisting of elements and make use of # operator to determine the length of the array and display the elements of the array using the length of the array in a for loop.
Code:
--defining an array called newarray
newarray = { 10, 30}
--determining the length of the array using # operator and displaying it as the output on the screen
arraylength = #(newarray)
print("The number of elements stored in the array is:\t", arraylength)
--displaying the elements stored in the array using for loop and length of the array
print("\nThe elements stored in the array are:\n")
for a = 1, arraylength do
print (newarray[a])
end
Output:
Explanation:
- In the above program, we are creating an array called newarray and storing elements inside the array. Then we are determining the length of the array using # operator and storing it in a variable called arraylength.
- Then we are displaying the length of the array as the output on the screen. Then we are making use of for loop and length of the array to display the elements of the array as the output on the screen. The output is shown in the snapshot above.
Example #3
Lua program to create an array consisting of elements and make use of # operator to determine the length of the array and display the elements of the array using the length of the array in a for loop.
Code:
--defining an array called newarray
newarray = {"Learning", "is", "fun"}
--determining the length of the array using # operator and displaying it as the output on the screen
arraylength = #(newarray)
print("The number of elements stored in the array is:\t", arraylength)
--displaying the elements stored in the array using for loop and length of the array
print("\nThe elements stored in the array are:\n")
for a = 1, arraylength do
print (newarray[a])
end
Output:
Explanation:
- In the above program, we are creating an array called newarray and storing elements inside the array. Then we are determining the length of the array using # operator and storing it in a variable called arraylength.
- Then we are displaying the length of the array as the output on the screen. Then we are making use of for loop and length of the array to display the elements of the array as the output on the screen. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “Lua array length” was beneficial to you. You can view EDUCBA’s recommended articles for more information.