May 12, 2021 Lua
Table is a data structure used by Lua to help us create different data types, such as numbers, dictionaries, and so on.
Lua table uses an array of associations, which you can index with any type of value, but this value cannot be nil.
Lua table is not a fixed size and you can expand it to your needs.
Lua also solves modules, packages, and objects through table. For example, string.format indicates the use of "format" to index the table string.
A constructor is an expression that creates and initializes a table. T ables are something unique and powerful to Lua. T he simplest constructor is , which is used to create an empty table. The array can be initialized directly:
-- 初始化表 mytable = {} -- 指定值 mytable[1]= "Lua" -- 移除引用 mytable = nil -- lua 垃圾回收会释放内存
When we set the element for table a and then assign a to b, a and b both point to the same memory. I f a is set to nil, b also has access to the table element. If no specified variable points to a, Lua's garbage collection mechanism cleans up the corresponding memory.
The following example demonstrates the above description:
-- 简单的 table mytable = {} print("mytable 的类型是 ",type(mytable)) mytable[1]= "Lua" mytable["wow"] = "修改前" print("mytable 索引为 1 的元素是 ", mytable[1]) print("mytable 索引为 wow 的元素是 ", mytable["wow"]) -- alternatetable和mytable的是指同一个 table alternatetable = mytable print("alternatetable 索引为 1 的元素是 ", alternatetable[1]) print("mytable 索引为 wow 的元素是 ", alternatetable["wow"]) alternatetable["wow"] = "修改后" print("mytable 索引为 wow 的元素是 ", mytable["wow"]) -- 释放变量 alternatetable = nil print("alternatetable 是 ", alternatetable) -- mytable 仍然可以访问 print("mytable 索引为 wow 的元素是 ", mytable["wow"]) mytable = nil print("mytable 是 ", mytable)
The above code execution results are:
mytable 的类型是 table mytable 索引为 1 的元素是 Lua mytable 索引为 wow 的元素是 修改前 alternatetable 索引为 1 的元素是 Lua mytable 索引为 wow 的元素是 修改前 mytable 索引为 wow 的元素是 修改后 alternatetable 是 nil mytable 索引为 wow 的元素是 修改后 mytable 是 nil
Here's a list of common ways table operations:
Serial number | Methods and uses |
---|---|
1 |
table.concat (table [, step [, start [, end]]]):
concat is an abbreviation for concatenate (chain, connection). The table.concat() function lists all elements in the parameter that specify the array portion of the table from the start position to the end position, separated by the specified separator (sep). |
2 |
table.insert (table, [pos,] value):
Insert an element with a value of value (pos) in the array portion of the table. Pos parameters are optional, defaulting to the end of the array section. |
3 |
table.maxn (table)
Specifies the largest key value of all positive key values in table. I f there is no element with a positive key value, 0 is returned. (The method no longer exists after Lua5.2, and this article uses a custom function implementation.) ) |
4 |
table.remove (table [, pos])
Returns the elements where the table array section is located in the pos position. T he elements that follow are moved forward. The pos parameter is optional, the default is table length, i.e. deleted from the last element. |
5 |
table.sort (table [, comp])
Ascending sorts a given table. |
Let's look at some examples of these methods.
We can use the concat() method to connect two table:
fruits = {"banana","orange","apple"} -- 返回 table 连接后的字符串 print("连接后的字符串 ",table.concat(fruits)) -- 指定连接字符 print("连接后的字符串 ",table.concat(fruits,", ")) -- 指定索引来连接 table print("连接后的字符串 ",table.concat(fruits,", ", 2,3))
The above code output results in:
连接后的字符串 bananaorangeapple 连接后的字符串 banana, orange, apple 连接后的字符串 orange, apple
The following example demonstrates the insertion and removal of table:
fruits = {"banana","orange","apple"} -- 在末尾插入 table.insert(fruits,"mango") print("索引为 4 的元素为 ",fruits[4]) -- 在索引为 2 的键处插入 table.insert(fruits,2,"grapes") print("索引为 2 的元素为 ",fruits[2]) print("最后一个元素为 ",fruits[5]) table.remove(fruits) print("移除后最后一个元素为 ",fruits[5])
The above code output results in:
索引为 4 的元素为 mango 索引为 2 的元素为 grapes 最后一个元素为 mango 移除后最后一个元素为 nil
The following example demonstrates the use of the sort() method for sorting Table:
fruits = {"banana","orange","apple","grapes"} print("排序前") for k,v in ipairs(fruits) do print(k,v) end table.sort(fruits) print("排序后") for k,v in ipairs(fruits) do print(k,v) end
The above code output results in:
排序前 1 banana 2 orange 3 apple 4 grapes 排序后 1 apple 2 banana 3 grapes 4 orange
Table.maxn After Lua5.2, the method no longer exists, and we define the table_maxn method implementation.
The following example shows how to get the maximum value in table:
function table_maxn(t) local mn = 0 for k, v in pairs(t) do if mn < k then mn = k end end return mn end tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"} print("tbl 长度 ", #tbl) print("tbl 最大值 ", table_maxn(tbl))
The above code output results in:
tbl 长度 3 tbl 最大值 26