Lua's table

Lua table

Table is a data structure of Lua used to help us create different data types, such as arrays and dictionaries.

Lua table uses associative arrays. You can use any type of value to index the array, but this value cannot be nil.

Lua table is not fixed in size, you can expand it according to your needs.

Lua also solves modules, packages, and objects through tables. For example, string.format means use "format" to index table string.

Table (table) structure

Constructors are expressions that create and initialize tables. The table is a powerful feature unique to Lua. The simplest constructor is {}, which is used to create an empty table. You can initialize the array directly:

 

When we set an element for table a and assign a to b, both a and b point to the same memory. If a is set to nil, then b can also access the elements of table. If no specified variable points to a, Lua's garbage collection mechanism will clean up the corresponding memory.

The following example demonstrates the above description:

 

 The above code execution result is:

Table operation

The following lists the commonly used methods of Table operation:

Serial number Method & Use
1 table.concat (table [, sep [, start [, end]]]):

concat is the abbreviation of concatenate (chain, connection). The table.concat () function lists all elements from the start position to the end position of the array part of the specified table in the parameter, and the elements are separated by the specified separator (sep).

2 table.insert (table, [pos,] value):

Insert an element whose value is value at the specified position (pos) in the array part of the table. The pos parameter is optional, and the default is the end of the array part.

3 table.maxn (table)

Specifies the largest key value among 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 article uses a custom function implementation )

4 table.remove (table [, pos])

Returns the element at the pos position of the table array. Subsequent elements will be moved forward. The pos parameter is optional, and the default is the table length, which is deleted from the last element.

5 table.sort (table [, comp])

Sort the given table in ascending order.

Next we look at some examples of these methods.

Table connection

We can use concat () to output a concatenated string of elements in a list:

 

 

Insert and remove

The following example demonstrates table insertion and removal operations:

 

Table sort

The following example demonstrates the use of sort () method to sort Table:

 

 

Table Maximum

table.maxn This method no longer exists after Lua5.2, we define the table_maxn method to achieve.

The following example demonstrates how to get the maximum value in table:

 

 The output of executing the above code is:

 Note: If the index is broken, the total number is incomplete.

 

 

Guess you like

Origin www.cnblogs.com/dalianpai/p/12698089.html