The table is a LUA data structure used to help us create different data types, such as arrays, dictionaries, and so on.
Lua table uses associative arrays, and you can use any type of value to count the index of the group, but this value cannot be nil.
The Lua table is not fixed in size, and you can expand it to suit your needs.
LUA also addresses modules (module), Packages (package), and objects (object) through a table. For example, String.Format represents the use of "format" to index a table string. Construction of table (tables)
Constructors are expressions that create and initialize tables. Tables are powerful things that are unique to Lua. The simplest constructor is {}, which is used to create an empty table. You can initialize an array directly:
--Initialize table
mytable = {}-
-Specify value
mytable[1]= "Lua"-
-Remove reference
mytable = nil-
-Lua garbage collection frees up memory
When we set the element for table A and then assign a to B, both A and B point to the same memory. If a is set to nil, B can also access the elements of the table. If no specified variable points to the A,lua garbage collection mechanism cleans up the corresponding memory.
The following examples illustrate the above description:
--Simple table
mytable = {}
print ("MyTable type is", type (mytable))
mytable[1]= "Lua"
mytable["wow"] = "Pre-Modified" The
elements of print ("MyTable index 1 are", mytable[1])
print ("MyTable index is wow", mytable["wow"))
--Alternatetable and my Table refers to the same table
alternatetable = mytable
print ("alternatetable index 1 element is", alternatetable[1])
print (" MyTable index for WOW elements is ", alternatetable[" wow "])
alternatetable[" wow "=" Modified "
print (" MyTable index is the element of Wow ", Myta ble["WOW"])-
-release variable
alternatetable = nil
print ("Alternatetable is", alternatetable)--
MyTable You can still access
print ("MyTable index is wow", mytable["wow"])
mytable = nil
print ("MyTable is", mytable)
The above code execution results are:
The mytable type is a table
mytable Index 1 element is the Lua
mytable Index to Wow is the element that is modified before
alternatetable index is 1 Elements of the Lua
mytable Index to WOW are the elements that are modified before
mytable index to WOW elements are modified
alternatetable are nil
MyTable Index is the element of Wow is modified after
mytable is nil
Table Action
The following is a list of common methods for table operations: Table Connection
We can use the concat () method to connect two table:
Fruits = {"Banana", "orange", "Apple"}
--Returns the string
print ("concatenated string", Table.concat (fruits) after the table connection)-
-Specify the connection character
print ("concatenated Strings", Table.concat (Fruits, ","))
--Specify the index to connect the table
print ("concatenated string", Table.concat (Fruits, ",", ","), 2, 3))
The results of the above code output are:
concatenated string bananaorangeapple concatenated string banana, Orange, Apple
connection after the string Orange, apple
inserting and removing
The following example demonstrates the insert and remove operations of a table:
Fruits = {"Banana", "orange", "Apple"}
--inserts
Table.insert (fruits, "mango") print at the end (
"element with index 4 as", fruits [4])
--Inserts
Table.insert (fruits,2, "grapes") print ("The element with
index 2 to", fruits[2])
print ("The last Element is") in the key at index 2, fruits [5])
Table.remove (fruits)
print ("The last element removed is", fruits[5])
The results of the above code output are:
An element with an index of 4 is an element with a mango index of 2 that is the last element of
grapes after the last element of Mango is
removed nil
Table Sort
The following example shows the use of the sort () method to sort the Table:
Fruits = {"Banana", "orange", "apple", "grapes"}
print ("sort before") for
k,v in ipairs (fruits) do
print (k,v)
End Table.sort (fruits)
print (after sort) for
k,v in ipairs (fruits) do
print (k,v)
The results of the above code output are:
Sort top
1 banana
2 Orange
3 Apple
4 grapes
sorted after
1 apple
2 Banana
3 grapes
4 Orange
Table max value
function Table_maxn (t) local
Mn=nil;
For K, v. in pairs (t) does
if (mn==nil) then
mn=v
End
If mn < v then
mn = v
End MN
End
tbl = {[1] = 2, [2] = 6, [3] = =5}
print ("tbl Maximum:", TABLE_MAXN (TBL))
print ("TBL Length", #tbl)
The results of the above code output are:
TBL Max:
tbl Length 3
Attention:
when we get the length of the table, whether using # or TABLE.GETN, it stops counting at the point where the index is interrupted, and the length of the table cannot be obtained correctly. You can use the following methods instead:
function Table_leng (t) local
leng=0
to K, V in pairs (t) does
leng=leng+1 end return
Leng;
End