LuaAboutTableThe function library is the content to be introduced in this article, mainly to learnTableFunction library inLuaFor more information, see the detailed description. PartTableThe function only affects the array, while the other part affects the entireTableAre affected.
- 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:gamma
- > print(table.concat(tbl, nil, 1, 2))
- alphabeta
- > print(table.concat(tbl, "\n", 2, 3))
- beta
- gamma
- table.insert(table, pos, value)
TableThe. insert () function specifies the position (pos) in the array part of the table to insert an element whose value is value. The pos parameter is optional. The default value is the end of the array part.
- > tbl = {"alpha", "beta", "gamma"}
- > table.insert(tbl, "delta")
- > table.insert(tbl, "epsilon")
- > print(table.concat(tbl, ", ")
- alpha, beta, gamma, delta, epsilon
- > table.insert(tbl, 3, "zeta")
- > print(table.concat(tbl, ", ")
- alpha, beta, zeta, gamma, delta, epsilon
- table.maxn(table)
Table. the maxn () function returns the maximum value of all positive key values 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 -- because the 26 and the previous numbers are not consecutive, they are not included in the array.
- > Print (table. maxn (tbl ))
- 26
- > Tbl [91.32] = true
- > Print (table. maxn (tbl ))
- 91.32
- Table. remove (table, pos)
Table. The remove () function deletes and returns the elements in 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, that is, the last element is deleted.
- 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 end
- > table.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
- end
- end
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 digit 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); -- equivalent to 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)
SetTableNumber of elements in.
Summary: AnalysisLuaAboutTableThe function library has been introduced. I hope this article will help you!