Updated April 11, 2023
Definition of Lua Table
The Lua table is the data structure. The table is the only data structure present in Lua Programming with the help of which we can create different structures like dictionary and array. Tables are associative arrays, which stores a set of key-value pairs. An associative array key-value pair stores a value for the key and later the value can be retrieved by using the key, that’s why the tables are also called dictionaries. Table size is not fixed, it can be grow required. The key or index can be a number, string but not nil. Tables in Lua programming are used in all representations even for the package representation, for example, if we access a method string. format, which means, the format function which is present in string package is accessing.
Syntax:
The syntax of table initialization –
table = {}
The syntax of table value assignment –
table[ key ]= "value"
The syntax of removing reference –
table = nil
Parameters:
- key – This is not an optional type parameter, that specifies the key or index for which the associated value stores. It can be number or string type but key does not support nil.
- value – This parameter specifies the value to be stored for the specific key.
How Table Structure works in Lua Programming?
The table data structure used in programming to create an array and dictionary. In Lua the table is created by {} as table = {}, which create an empty table or with elements to create non-empty table. After creating a table an element can be add as key-value pair as table1[key]= “value”.
Examples
Examples for the table structure in programming.
Example #1
Lua programming for creating and initializing a table
Code:
-- create an empty table and assign it to variable "table1"
table1 = {}
print("The table is : ", table1)
print("The type of table is ", type(table1))
table1[1]= "Apple"
table1["2"] = "Banana"
table1["Three"] = "Cherry"
print("Table value at index 1 is : ", table1[1])
print("Table value at index '2' is : ", table1["2"])
print("Table value at index 'Three' is : ", table1["Three"])
-- cptable and table1 refers to same table
cptable = table1
print("cptable value at index 1 is : ", table1[1])
print("cptable value at index '2' is : ", table1["2"])
print("cptable value at index 'Three' is : ", table1["Three"])
-- cptable key 1 value change
cptable[1] = "Grape"
print("Table value at index 1 is : ", table1[1])
print("cptable value at index 1 is : ", table1[1])
cptable["2"] = nil
print("cptable value released at index '2' is : ", table1["2"])
-- only variable released but not table
cptable = nil
print("cptable is released : ", cptable)
print("The table is : ", table1)
-- table1 is still accessible
print("Table value at index '2' is : ", table1["2"])
table = nil
print("Table is : ", mytable)
Output:
As in the above Lua program code, an empty table is created and initialized by using the key-value pairs, where 1 “2”, “Three” are keys and “Apple”, “Banana”, “Cherry” are the associative values, which are printing farther by using the keys, as we can see in the output. Farther the table reference or copy is created, so the copy of the table also contain the same elements. When the copy of the table is deleted, the only copy table is deleted and the original table is still there. And farther also we can see that if we released the single element, then only the single element get deleted not the structure.
Example #2
Lua programming for creating an array
Code:
-- create a city table as an array and assign values to it
city = { "Bangalore", "Delhi", "Mumbai", "Hyderabad" }
print("The city table is : ", city)
print("The type of city table is ", type(city) )
-- still table is a key-value association.
print("Table value at index 1 is : ", city[1] )
print("Table value at index 2 is : ", city[2] )
print("Table value at index 3 is : ", city[3] )
print("The length of the city array is : ", #city )
-- mix the array syntax with the usual key-value syntax
city = { "Bangalore", "Delhi", city = "Mumbai", [123] = "Hyderabad" }
-- print key value in for loop
for key, value in pairs(city) do print( key, value ) end
-- the length of an array get by using the # operator
print("The length of the city array is : ", #city )
-- table.insert to insert an element
table.insert( city, 3, "Goa" )
-- print key value in for loop
print("The city values after Insert :")
for key, value in pairs(city) do print( key, value ) end
-- table.remove to remove an element
table.remove( city, 1 )
-- print key value in for loop
print("The city values after remove :")
for key, value in pairs(city) do print( key, value ) end
Output:
As in the above Lua program code, a non-empty table is created that is a city without any keys as an array, it takes index number for the key by default, so 1 2, 3,4 are keys and “Bangalore”, “Delhi”, “Mumbai”, “Hyderabad” are the associative values, which are printing farther by using the keys, as we can see in the output. Farther the table length is printing by using “#” operator. And farther also we can see that the element is added and the element is removed from an array.
Conclusion
The Lua table is a built-in data structure is in Lua programming, which is used to create an array and dictionary in Lua.
Recommended Articles
We hope that this EDUCBA information on “Lua Table” was beneficial to you. You can view EDUCBA’s recommended articles for more information.