table
该库提供了用于表操作的通用函数。它在表 table 内部提供其所有函数。
🎒 示例
待添加示例
🗿 静态函数
| Returns | Name | Description | |
|---|---|---|---|
| number | insert | 在 tbl 的 position 位置插入元素值 | |
sort | 对 tbl 元素进行就地排序,排序范围从 tbl[1] 到 tbl[#tbl] | ||
| table | move | 将元素从 source_table 移动到 dest_table | |
| any | remove | 从 tbl 中移除位置 index 处的元素,并返回被移除元素的值 | |
| string | concat | 给定一个所有元素都是字符串或数字的 tbl,返回元素拼接后的字符串。 |

insert
在tbl的position位置插入元素值,并将元素tbl[position], tbl[position + 1], ···, tbl[#tbl]向上移动
local ret = table.insert(tbl, position?, value?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| table | tbl | Required parameter | 要插入变量的表。 |
| number | position? | #tbl + 1 | 表中插入变量的位置。如果第三个参数为 nil,则此参数将作为要插入到给定表末尾的值。 |
| any | value? | | 要插入到表中的变量。 |
Returns
| Type | Description |
|---|---|
| number | No description provided |

sort
对tbl元素进行就地排序,排序范围从tbl[1]到tbl[#tbl]。如果给定了sorter,则它必须是一个接收两个tbl元素的函数,并且当最终顺序中第一个元素必须排在第二个元素之前时返回 true,这样在排序后,i <= j 意味着not sorter(tbl[j], tbl[i])。如果没有给定sorter,则使用标准 Lua 运算符 < 代替。sorter函数必须定义一致的顺序;更正式地说,该函数必须定义严格弱序。(弱序类似于全序,但为了比较的目的,它可以将不同的元素视为相等)。
该排序算法是不稳定的:由给定顺序判定为相等的不同元素,其相对位置可能会在排序后发生改变。
table.sort(tbl, sorter)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| table | tbl | Required parameter | 要排序的表。 |
| function | sorter | Required parameter | 如果指定了该参数,此函数在调用时将分别传入 2 个参数。如果你希望在排序后的数组中第一个参数排在前面,请在此函数中返回 true。 |

move
将元素从source_table移动到dest_table,其效果等同于以下多重赋值:dest_table[dest], ··· = a1[from], ···, source_table[to]。
目标范围可以与源范围重叠。要移动的元素数量必须能容纳在 Lua 整数中。
local ret = table.move(source_table, from, to, dest, dest_table?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| table | source_table | Required parameter | 要从中移动元素的源表。 |
| number | from | Required parameter | 要移动的源范围的起始索引。 |
| number | to | Required parameter | 要移动的源范围的结束索引。 |
| number | dest | Required parameter | 应将移动的元素插入到目标表中的索引位置。如果未指定,移动的元素将被插入到表的末尾。 |
| table | dest_table? | source_table | 元素要移动到的目标表。默认情况下,它与源表相同。 |
Returns
| Type | Description |
|---|---|
| table | No description provided |

remove
从tbl中移除位置index处的元素,并返回被移除元素的值。
当index是 1 到#tbl之间的整数时,它会向下移动元素tbl[index + 1], tbl[index + 2], ···, tbl[#tbl]并擦除元素tbl[#tbl]。
当#tbl为 0 时,index也可以是 0,或者是#tbl + 1。
local ret = table.remove(tbl, index)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| table | tbl | Required parameter | 要删除值的表。 |
| number | index | Required parameter | 要删除的值的索引。 |
Returns
| Type | Description |
|---|---|
| any | No description provided |

concat
给定一个所有元素都是字符串或数字的tbl,返回字符串tbl[start_pos] .. separator .. tbl[start_pos + 1] ... separator .. tbl[end_pos]。
如果start_pos大于end_pos,则返回空字符串
local ret = table.concat(tbl, separator?, start_pos?, end_pos?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| table | tbl | Required parameter | 要拼接的表。 |
| string | separator? | "" | 要在字符串之间插入的分隔符 |
| number | start_pos? | 1 | 起始索引 |
| number | end_pos? | #tbl | 结束索引 |
Returns
| Type | Description |
|---|---|
| string | No description provided |