Updated April 20, 2023
Definition of Lua Table Length
In Lua we can find the length of the table by writing the code manually, in short in Lua we do not have any method or function to get the length of the table directly, we have to write code and count each object manually in Lua. If we talk about the table in Lua it is a very powerful data structure in Lua, and internally it implements the array. We can use pair() to iterate the table objects and count the length of the table for us. In the coming section, we will discuss more the internal working of table and length in Lua to get a better understanding and implementation while writing the program in Lua.
Syntax:
As we discussed that we do not have any function which directly gives us the length of the table in Lua, instead we have to use manual code to get the length of the table. Let’s take a look at its syntax how we can use it see below;
pair(your_table_object)
As you can see in the above lines of syntax we are using pair() to count the length of the table in Lua. Let’s see one practice example where we can understand its implementation of the syntax see below;
e.g.;
pair(demotabl)
In the coming section of the tutorial we will discuss the internal working of table length, and also the piece of code we required to get the length of the table.
How to find Table Length in Lua?
As we already know that tables are a very powerful data structure in Lua, which internally uses an associative array in lua. We also know that tables in Lua have no fixed size or length, we can add as many objects to it we want. In Lua, we uses tables to represent our queue, set, array and other data structure we have available. we can assign an index to any string or other types that are supported in Lua it is not only specific to number only but it should be unique and not nil. If we want to declare a table in Lua then we have no specific way for that, also Lua does not create a copy of the table in the background. Let’s take an example of how we can create a table in Lua;
Table creation: If you want to create a table in Lua then we have to use constructor expression for this. It is the simplest for to create a table in Lua and easy to handle. Below is the constructor expression which will help us to create our first table in Lua see below;
e.g. :
name_of_table = {}
As you can see in the above lines of code we are using {} ‘curly braces’ to create a table in Lua, it is very simple. In order to find the length of the table we first have to assign few values to it. We can use string or number or any other type from Lua to create the index of the table. These table elements are not values or keys but rather they are objects in Lua like any other programming language java etc. Now let’s see the syntax to assign the value in order to get the size or length of the table in lua see below;
e.g. :
table1 = {"hello", "bye", "test", "end"}
In the above lines, we are assigning few values to the table. But if you want to assign a specific index then follow below syntax;
e.g. :
table1 = {}
table1[10] = "i am ten"
In the above lines of code, we are assigning the value ‘i am ten’ at the index ’10’ here and so on. After initializing so many values and index for your table now we can calculate the length of the table by using an iterator, see below;
e.g. :
for _ in pairs(table_name) do logic to count the object ges here .. end
In the above piece of syntax, we are trying to get the length of the table in Lua. For this, we are using for with pair(), inside this we will pass the table object, followed by the manual code which will count the number of objects. To count the object we can create a local variable which will increase every time with the object iteration in Lua. Once the iteration of the table objects gets completed it will give us the length of the table. In the coming section of the tutorial, we will see a working example of the length of the table, but before that, we will go through some points which need to be taken care of table length see below;
1) Remember in Lua we do not have any function or we can say direct function to get the size or length of the table directly.
2) we need to write the code manually to get the length of the table in Lua.
3) For this we can use pair() which will iterator the table object and give us the desired result.
4) Table is a powerful data structure that does not require any external library to use because it is in build in lua.
Example
In this example we are trying to get the length of the table, for this we have defined every type of table, after that we are using pair() function passing the table object inside it to get the length by iterating the table object.
Code:
print("Demo to count the length of table in Lua !!!")
local mytabl1 = {"hello", "bye", "me", "world", "joy", "enjoy"}
local mytabl2 = {100, 200, 300, 400, 500, 600, 700, 800, 0}
local mytabl3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
local mytabl4 = {1.0, 2.0, 3.0, 40.5, 56.9, 5.2, 6.4, 34.9}
local mytabl5 = {"A", "B", "C", "R", "Y", "E", "D", "P", "I", "U", "T"}
print("calculating size or length of each table separately !!")
print("results are::")
size1 = 0
for _ in pairs(mytabl1) do size1 = size1 + 1 end
print("size of first table is ::" , size1)
size2 = 0
for _ in pairs(mytabl2) do size2 = size2 + 1 end
print("size of second table is ::" , size2)
size3 = 0
for _ in pairs(mytabl3) do size3 = size3 + 1 end
print("size of third table is ::" , size3)
size4 = 0
for _ in pairs(mytabl4) do size4 = size4 + 1 end
print("size of fourth table is ::" , size4)
size5 = 0
for _ in pairs(mytabl5) do size5 = size5 + 1 end
print("size of fifth table is ::" , size5)
Output:
Conclusion
By the use table we can make the processing of large data easy and fast because it is and data structure. To get the length we have already write the manual code, no direct function supported by lua.
Recommended Articles
We hope that this EDUCBA information on “Lua Table Length” was beneficial to you. You can view EDUCBA’s recommended articles for more information.