This article mainly introduces the concept of the table in Lua and its related methods of operation, is the basic knowledge in the LUA introductory study, the need for friends can refer to the
The table is the only data structure in which LUA can help us create different types, such as arrays and dictionaries. LUA uses associative arrays and can be not only numeric but also has different 0 string indices. Tables are not fixed in size and can grow as needed.
All the statements used by LUA, including the representative tables for packaging. When we access a string of methods. format, which means that we are accessing the formatting features of the string encapsulation.
Representation and usage
Tables are called objects and they are neither worth nor changing. LUA creates an empty table using the constructor expression {}. It is to be known that there is no fixed relationship between the reference to the Save table and the variables of the table itself.
The code is as follows:
--sample Table Initialization
MyTable = {}
--simple Table Value Assignment
mytable[1]= "Lua"
--removing Reference
MyTable = Nil
--Lua garbage collection'll take care of releasing memory
When we have a table with the elements of the collection, if we designate them as B,a and b all point to the same memory. No separate memory is allocated separately to B. When set to none, the table will still have access to B. When the tables are not referenced, then the cleanup process is required in the LUA garbage collection so that the unreferenced memory is reused again.
An example is shown below to illustrate the above characteristics of the table.
The code is as follows:
--Simple empty table
MyTable = {}
Print ("Type of MyTable is", Type (MyTable))
mytable[1]= "Lua"
mytable["wow"] = "Tutorial"
Print ("MyTable Element at index 1", mytable[1])
Print ("MyTable Element at index wow is", mytable["wow"])
--alternatetable and mytable refers to same table
Alternatetable = MyTable
Print ("Alternatetable Element at index 1", alternatetable[1])
Print ("MyTable Element at index wow is", alternatetable["wow"])
alternatetable["wow"] = "I changed it"
Print ("MyTable Element at index wow is", mytable["wow"])
--only variable released and not table
Alternatetable = Nil
Print ("Alternatetable is", alternatetable)
--MyTable is still accessible
Print ("MyTable Element at index wow is", mytable["wow"])
MyTable = Nil
Print ("MyTable is", mytable)
When we run the above program, we get the following output
The code is as follows:
Type of mytable is table
MyTable Element at index 1 is Lua
MyTable Element at index wow is Tutorial
Alternatetable Element at index 1 is Lua
MyTable Element at index wow is Tutorial
MyTable Element at index wow is I changed it
Alternatetable is nil
MyTable Element at index wow is I changed it
MyTable is nil
Table Operations
Manipulate the built-in functions on the table and they are listed in the following table.
Let's look at some examples of the above functions.
Table concatenation
We can use the CONCAT function to connect, like the two tables shown below.
The code is as follows:
Fruits = {"Banana", "orange", "Apple"}
--Returns concatenated string of table
Print ("concatenated string", Table.concat (fruits))
--concatenate with a character
Print ("concatenated string", Table.concat (Fruits, ","))
--concatenate Fruits based on index
Print ("concatenated string", Table.concat (Fruits, ",", 2,3))
When we run the above program, we get the following output
The code is as follows:
concatenated string Bananaorangeapple
concatenated string banana, orange, apple
concatenated string Orange, apple
Inserting and deleting
Inserts the items in the table and removes the most common table manipulations. The explanations below it.
The code is as follows:
Fruits = {"Banana", "orange", "Apple"}
--Insert a fruit at the end
Table.insert (Fruits, "mango")
Print ("Fruit at index 4", fruits[4])
--insert Fruit at index 2
Table.insert (fruits,2, "grapes")
Print ("Fruit at index 2", fruits[2])
Print ("The maximum elements in table is", TABLE.MAXN (fruits))
Print ("The last element is", fruits[5])
Table.remove (fruits)
Print ("The previous last element is", fruits[5])
When we run the above program, we get the following output
The code is as follows:
Fruit at index 4 is mango
Fruit at index 2 is grapes
The maximum elements in table is 5
The last element is Mango
The previous last element is nil
Sort table
Sort tables usually require and sort the elements in a table of functions alphabetically. An example of this is shown in the following illustration.
The code is as follows:
Fruits = {"Banana", "orange", "apple", "Grapes"}
For k,v in ipairs (fruits) do
Print (K,V)
End
Table.sort (fruits)
Print ("Sorted table")
For k,v in ipairs (fruits) do
Print (K,V)
End
When we run the above program, we get the following output
The code is as follows:
1 banana
2 Orange
3 Apple
4 grapes
Sorted table
1 apple
2 Banana
3 grapes
4 Orange