Table library functions
Table. Concat (table, SEP, start, end)
Concat is the abbreviation of concatenate (chained, connected), table. the Concat () function lists all elements in the array part of the specified table in the parameter from the start position to the end position, and the elements are separated by the specified separator (SEP. Except table, other parameters are not required. The default Delimiter is a null character, the default start value is 1, and the default end value is the total length of the array.
The SEP, start, and end parameters are read in sequence. Therefore, although they are not required, to specify backend parameters, you must specify the preceding parameters.
tbl = {"alpha", "beta", "gamma"}print(table.concat(tbl, ":"))-- alpha:beta:gammaprint(table.concat(tbl, nil, 1, 2))--alphabetaprint(table.concat(tbl, "\n", 2, 3))--beta--gamma
Table. insert (table, POs, value)
The table. insert () function specifies the position (POS) in the array part of the table to insert an element with a value. The POS parameter is optional. The default value is the end of the array.
tbl = {"alpha", "beta", "gamma"}table.insert(tbl, "delta")table.insert(tbl, "epsilon")print(table.concat(tbl, ", ")--alpha, beta, gamma, delta, epsilontable.insert(tbl, 3, "zeta")print(table.concat(tbl, ", ")--alpha, beta, zeta, gamma, delta, epsilon
Table. maxn (table)
The table. maxn () function returns the maximum value of all positive keys in the specified table. If no element with a positive key value exists, 0 is returned. This function is not limited to the array part of the table.
tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"}print(#tbl)-- 3,因为26和之前的数字不连续, 所以不算在数组部分内print(table.maxn(tbl))--26tbl[91.32] = trueprint(table.maxn(tbl))--91.32
Table. Remove (table, POS)
The table. Remove () function deletes and returns the elements at the POs position of the table array. The subsequent elements are moved forward. The POS parameter is optional. The default value is the table length. Delete the last element.
Table. Sort (table, comp)
The table. Sort () function sorts the given table in ascending order.
tbl = {"alpha", "beta", "gamma", "delta"}table.sort(tbl)print(table.concat(tbl, ", "))--alpha, beta, delta, gamma
Comp is an optional parameter. this parameter is an external function and can be used to customize the sorting standard of the sort function.
This function must meet the following conditions: accept two parameters (A and B in sequence) and return a Boolean value. When a should be placed before B, true is returned, otherwise, false is returned.
For example, when we need to sort in descending order, we can write as follows:
sortFunc = function(a, b) return b < a endtable.sort(tbl, sortFunc)print(table.concat(tbl, ", "))--gamma, delta, beta, alpha
Similar principles can also be used to write more complex sorting functions. For example, a table contains the names and levels of three members of a trade union:
guild = {}table.insert(guild, { name = "Cladhaire", class = "Rogue", level = 70,})table.insert(guild, { name = "Sagart", class = "Priest", level = 70,})table.insert(guild, { name = "Mallaithe", class = "Warlock", level = 40,})
When sorting this table, apply the following rules: sort by level in ascending order, and sort by name in ascending order at the same level.
You can write such a sorting function:
function sortLevelNameAsc(a, b) if a.level == b.level then return a.name < b.name else return a.level < b.level endend
The test function is as follows:
table.sort(guild, sortLevelNameAsc)for idx, value in ipairs(guild) do print(idx, value.name) end--1, Mallaithe--2, Cladhaire--3, Sagart
Table. foreachi (table, function (I, V ))
A continuous Integer Range starting from 1 (number 1) is expected to traverse the key and value pairs in the table for function (I, V) operations.
t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};table.foreachi(t1, function(i, v) print (i, v) end) ; --等价于foreachi(t1, print)
Output result:
1 2
2 4
3 6
4 8
5 10
6 12
Table. foreach (table, function (I, V ))
Different from foreachi, foreach performs iteration on the entire table.
t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};table.foreach(t1, function(i, v) print (i, v) end) ;
Output result:
1 2
2 4
3 6
4 8
5 10
6 12
Web Hello Lua
Language Lua
Version 5
Table. getn (table)
Returns the number of elements in the table.
t1 = {1, 2, 3, 5};print(getn(t1))--4
Table. setn (table, nsize)
Set the number of elements in the table
Table library functions