Updated April 5, 2023
Definition of Lua Print Table
Lua is a programming language that is extensible as well as lightweight. This language is in C and designed from the start to be software that can be integrated into C and other conventional programming languages. In Lua, tables are considered as the one and only data structure that creates several types such as dictionaries and arrays. Moreover, it uses associative arrays that can be indexed with numbers and strings as well. The specialty of the table is that it does not have any fixed size or it can be grown based on the requirement. Tables in Lua are used in almost all representations like package representations.
Syntax:
Tables in Lua are neither variables nor values. Instead, they are considered as objects and it uses a {}, also known as constructor expression for the creation of an empty table.
— table initialization
tb = {}
— value assignment in table
tb[1]= "Hai"
–remove the reference
tb = nil
Here, tb is the table name.
How to print tables in Lua?
Now, we will see how to print tables in Lua.
As already discussed, we can create empty tables and tables with string as well as numerical indexes. First, we will see how to print the type of table.
-- create an empty table
tb = {}
print("The type of the table created is: ",type(tb))
Using the above commands, an empty table is created and its type can be identified using the type() method.
Now, we will see how to set the value “one” in index 1 and value “Apple” in the index fruit of the table.
tb[1]= "one"
tb["fruit"] = "Apple"
print("Element in the table which is at index 1 is ", tb[1])
print("Element in the table which is at index 1]fruit is ", tb["fruit"])
Finally, print() is used for printing the tables.
Methods for Table Manipulation
Below are some of the methods in Lua that are used for table manipulation.
- concat(table [, sep [, i [, j]]])
Strings in the table will be concatenated based on the given parameters.
- insert (table, [pos,] val)
Value val will be inserted into the table at the mentioned position.
- maxn (table)
Numeric index which is the largest will be returned.
- remove (table [, pos])
Values will be removed from the table.
- sort (table [, comp])
Table will be sorted based on the comparator argument which is optional.
Examples
Now, we can see some sample programs on Lua tables.
Example #1: Lua program to create a table and print elements
-- create an empty table
tb = {}
print("The type of the table created is: ",type(tb))
tb[1]= "one"
tb["fruit"] = "Apple"
print("Element in the table which is at index 1 is ", tb[1])
print("Element in the table which is at index 1]fruit is ", tb["fruit"])
Sample Output:
In this program, an empty table is created and elements are added to index 1 and ‘fruit’.
Example #2: Lua program to create a new table and refer it to old table
-- create an empty table
tb = {}
print("The type of the table created is: ",type(tb))
tb[1]= "one"
tb["fruit"] = "Apple"
print("Element in the table which is at index 1 is ", tb[1])
print("Element in the table which is at index 1]fruit is ", tb["fruit"])
--create another table and refer it to the table which is created first
tbnew = tb
print("Element at index 1 in the table tbnew is ", tbnew[1])
print("Element at index fruit in the table tbnew is ", tbnew["fruit"])
tbnew[1] = "two"
print("Element at index 1 in the table tbnew is ", tbnew[1])
Sample Output:
In this program, an empty table is created and elements are added to index 1 and ‘fruit’. Later, a new table is created and it is referred to the old one. Then, elements at the index are printed.
Example #3: Lua program to concatenate elements
Animals = { "Horse" , "Buffalo" , "Cat" , "Deer" }
-- Print the concatenated string of the table
print("The string which is concatenated is ",table.concat(Animals))
-- Print the concatenated character
print("The string after concatenating with / is ",table.concat(Animals,"/ "))
-- Print the concatenated strings based on index
print("The string after concatenating based on index is: ",table.concat(Animals,"/ ", 1,4))
Sample Output:
In this program, a table is created and elements are concatenated using concat() method.
Example #4: Lua program to insert new elements
Animals = { "Horse" , "Buffalo" , "Cat" , "Deer" }
-- insert string at the end
table.insert(Animals,"Dog")
--insert string at index 4
table.insert(Animals , 4 , "Cow" )
-- Print the concatenated string of the table
print("The string which is concatenated is ",table.concat(Animals))
-- Print the concatenated character
print("The string after concatenating with / is ",table.concat(Animals,"/ "))
-- Print the concatenated strings based on index
print("The string after concatenating based on index is: ",table.concat(Animals,"/ ", 1,4))
Sample Output:
In this program, a table is created and elements are inserted into it. Later, they are concatenated using concat() method.
Example #5: Lua program to find the maximum number of elements
Animals = { "Horse" , "Buffalo" , "Cat" , "Deer" }
-- insert string at the end
table.insert(Animals,"Dog")
--insert string at index 4
table.insert(Animals , 4 , "Cow" )
-- Print the concatenated string of the table
print("The string which is concatenated is ",table.concat(Animals))
-- Print the concatenated character
print("The string after concatenating with / is ",table.concat(Animals,"/ "))
-- Print the concatenated strings based on index
print("The string after concatenating based on index is: ",table.concat(Animals,"/ ", 1,4))
print("The maximum number of items in the Animals table is",table.maxn(Animals))
print("The last item in the Animals table is", Animals[6])
--remove the table
table.remove(Animals)
Sample Output:
In this program, a table is created and a maximum number of elements is identified. At last, the table is removed.
Conclusion
Tables in Lua are considered as the one and only data structure that creates several types such as dictionaries and arrays. In this article, different aspects such as syntax, methods, working, and examples of Lua tables are explained in detail.
Recommended Articles
We hope that this EDUCBA information on “Lua print table” was beneficial to you. You can view EDUCBA’s recommended articles for more information.