Lua table
table
is a data structure of Lua to help us create different data types,such as arrays, dictionaries, etc.
Lua table uses associative arrays. You can index the array with any type of value, but this value cannot be nil
.
Lua table is not fixed in size, so you can expand it according to your needs.
Lua also solves modules (module), packages (package), and objects (Object) through table. For example string.format
indicates that table string isindexed using “format”.
Construction of table
A constructor is an expression that creates and initializes a table. The watch is Lua
something unique and powerful. The simplest constructor is``{}`` to create an empty table You can initialize the array directly:
-- initialization table
mytable = {}
-- specified value
mytable[1]= "Lua"
-- Remove Reference
mytable = nil
-- lua Garbage collection releases memory
When we table an and set the element, and then assign a to b, both an and b point to the same memory. If an is set to nil
then b can also access the element of table
. If there is no specified variable pointing to the garbage collection mechanism of aPhine Lua, the corresponding memory will becleaned up.
The following example demonstrates the above description:
Example
-- Simple table
mytable = {}
print("The type of mytable is ",type(mytable))
mytable[1]= "Lua"
mytable["wow"] = "Before modification"
print("The element with mytable index 1 is ", mytable[1])
print("The element with mytable index wow is ", mytable["wow"])
-- alternatetable and mytable refer to the same table
alternatetable = mytable
print("The element with an alternatetable index of 1 is ", alternatetable[1])
print("The element with mytable index wow is ", alternatetable["wow"])
alternatetable["wow"] = "After modification"
print("The element with mytable index wow is ", mytable["wow"])
-- Release variables
alternatetable = nil
print("alternatetable is ", alternatetable)
-- mytable can still be accessed
print("The element with mytable index wow is ", mytable["wow"])
mytable = nil
print("mytable is ", mytable)
The result of the above code execution is:
The type of mytable is table
The element with mytable index 1 is Lua
The element with mytable index wow is Before modification
The element with an alternatetable index of 1 is Lua
The element with mytable index wow is Before modification
The element with mytable index wow is Before modification
Alternatetable is nil
The element with mytable index wow is Before modification
Mytable is nil
Table operation
The following is a list of common methods for Table operations:
Serial number |
Method and use |
---|---|
1 |
Table.concat (table [, sep [, start [, end] ]): concat is the abbreviation of concatenate (linkage, connection). The table.concat () function lists allelements of the array portion of the specified table in the parameter from the start position to the end position, separated by the specified sep. |
2 |
Table.insert (table [pos,] Value): insert an element with a value of value at the specified position (pos) in the array section of table. The pos parameter is optional and defaults to the end of the array section. |
3 |
Table.maxn (table) specifies the largest key value of all positive key values in table. If there is no element with a positive key value, 0 is returned. (this method no longer exists after Lua5.2. This paper uses a custom function to implement it.) |
4 |
Table.remove (table [, pos] Returns the element where part of the table array is in the pos position. Subsequent elements are moved forward. The posparameter is optional. The default is table length, which is deleted from the last element. |
5 |
Table.sort (table [, comp] Sort the given table in ascending order. |
Next, let’s take a look at examples of these methods.