约 4475 字

约 22 分钟

#Lua 的 Table 模块

函数说明
table.concat将表中的元素进行拼接
table.insert向表中插入元素
table.move将表中的元素移动到另一个表
table.pack将一组数据打包成一个表
table.remove从表中删除一个数据
table.sort排序
table.unpack将表中的元素解包

#table.concat

table.concat (list [, sep [, i [, j]]])

说明

将表中的元素进行拼接,返回字符串 list[i]..sep..list[i+1] ··· sep..list[j]。

参数

  • list - 要进行拼接的表,元素的类型必须是字符串或数值
  • sep - 拼接时相邻元素之间插入的字符串,默认值是空串
  • i - 拼接元素的起始索引;默认值是 1,即表的第一个元素
  • j - 拼接元素的终止索引;默认值是 #list,即表的最后一个元素

示例

local array = {"never", "gonna", "give", "you", "up"}
print(table.concat(array))                  -- 直接拼接
print(table.concat(array, " "))             -- 元素之间插入空格
print(table.concat(array, " ", 3, 4))       -- 选取部分元素

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#table.insert

table.insert (list, [pos,] value)

说明

在表中索引为 pos 的位置插入值为 value 的元素。

参数

  • list - 要进行插入的表
  • pos - 要插入的位置索引;默认为 #list+1,即在表的末尾追加元素
  • value - 要插入的值

示例

local array = {1, 2, 3, 4, 5}       -- 定义数组
print(table.concat(array, ","))     -- 查看数组

table.insert(array, 6)              -- 在末尾追加元素
print(table.concat(array, ","))     -- 查看数组

table.insert(array, 3, 10)          -- 在索引 3 插入元素
print(table.concat(array, ","))     -- 查看数组

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#table.move

table.move (a1, f, e, t [,a2])

说明

将表 a1 中的元素移动到表 a2 中,相当于 a2[t],··· = a1[f],···,a1[e]。

参数

  • a1 - 源表
  • f - 要移动的第一个元素的索引
  • e - 要移动的最后一个元素的索引
  • t - 接收第一个元素的索引
  • a2 - 目标表;默认为 a1,即在 a1 表内进行移动

示例

local src = {1, 2, 3, 4, 5}         -- 定义源数组
local dst = {10, 20, 30, 40, 50}    -- 定义目标数组

table.move(src, 2, 4, 2, dst)       -- 将 src 数组的 2 至 4 元素移动到 dst 数组的索引 2 位置
print(table.concat(dst, ","))       -- 查看数组

table.move(src, 2, 4, 3)            -- 将 src 的 2 至 4 元素移动到自身的索引 3 位置
print(table.concat(src, ","))       -- 查看数组

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#table.pack

table.pack (···)

说明

将参数打包成一个表,并将表的 n 字段设为参数的数量。

示例

local result = table.pack('a', 'b', 'c', 'd', 'e')
print(result.n)                     -- 查看表的字段 n
print(table.concat(result, ","))    -- 查看数组

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#table.remove

table.remove (list [, pos])

说明

从表 list 中删除索引为 pos 的元素,之后的元素向前移动。

参数

  • list - 要进行删除的表
  • pos - 要插入的位置索引;默认为 #list,即删除最后一个元素

示例

local array = {1, 2, 3, 4, 5}       -- 定义数组
table.remove(array)                 -- 删除最后一个元素
print(table.concat(result, ","))    -- 查看数组
table.remove(array, 2)              -- 删除索引为 2 的元素
print(table.concat(result, ","))    -- 查看数组

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#table.sort

table.sort (list [, comp])

说明

将表 list 进行排序,会修改 list。

参数

  • list - 要进行排序的表
  • comp - 用于元素比较的函数,必须接收两个参数,第一个参数排在第二个参数之前时返回 true,反之返回 false;默认为升序排序

示例

local array = {3, 7, 2, 4, 6, 1, 5}     -- 定义数组
table.sort(array)                       -- 默认(升序)排序
print(table.concat(array, ","))         -- 查看数组

function gt(x, y)
    if x > y then
        return true
    else
        return false
    end
end

table.sort(array, gt)                   -- 降序排序
print(table.concat(array, ","))         -- 查看数组

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#table.unpack

table.unpack (list [, i [, j]])

说明

将表 list 的元素进行解包,相当于 return list[i], list[i+1], ···, list[j]。

参数

  • list - 要解包的表
  • i - 要解包的第一个元素;默认为 1,即第一个元素
  • j - 要解包的最后一个元素;默认为 #list,即最后一个元素

示例

local array = {1, 2, 3, 4, 5}               -- 定义数组
local x,y,z = table.unpack(array, 2, 4)     -- 解包 2 至 4 元素
print(x, y, z) 

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#推荐阅读

Table Manipulation - Lua 5.4 Reference Manual

更新: 2026/2/6

作者: PlanC

创建: 2025/10/30