找回密码
 register

QQ登录

只需一步,快速开始

搜索
查看: 7|回复: 0

[每天自学] 天龙八部源端 x64:Lua4 增加 Lua5 的 table.xxx 用法

[复制链接]
  • 打卡等级:本地老炮
  • 打卡总天数:384
  • 打卡月天数:1
  • 打卡总奖励:382
  • 最近打卡:2026-01-01 00:21:32
Waylee 发表于 2026-1-1 00:21 | 显示全部楼层 |阅读模式 | Google Chrome | Windows 10

马上注册,查看网站隐藏内容!!

您需要 登录 才可以下载或查看,没有账号?register

×

源端 Lua4 自带一套 table 相关函数,但很多脚本作者更习惯 Lua5 的写法(table.concat / table.sort / table.pack / table.unpack 这些)。我这里的做法很简单:不动原来的 Lua4 行为,把常用函数塞进去,让脚本能直接按 Lua5 的习惯写。


新增 table 库函数一览

函数 用途 返回值
table.concat(t [, sep [, i [, j]]]) 把数组表拼成字符串 拼接后的字符串
table.sort(t [, comp]) 对数组表排序(原地排序)
table.insert(t, [pos,] value) 插入元素(pos 不写默认尾部)
table.remove(t [, pos]) 删除元素(pos 不写默认尾部) 被删除的元素 / 空
table.getn(t) 获取长度(Lua4 习惯函数) 数字
table.setn(t, n) 设置长度(Lua4 习惯函数)
table.foreach(t, func) 遍历(Lua4 老写法) func 非 nil 时返回该值,否则无
table.foreachi(t, func) 遍历数组部分(Lua4 老写法) func 非 nil 时返回该值,否则无
table.pack(...) 把参数打包成数组表(Lua5 常用) table(带 t.n 字段)
table.unpack(t [, i [, j]]) 把数组表解包成多返回值(Lua5 常用) 多返回值

用法示例

1)table.concat:拼字符串

local t = {"TLBB", "Lua4", "Lua5", "table"}
print(table.concat(t, " | "))
-- TLBB | Lua4 | Lua5 | table

带范围:

print(table.concat(t, "-", 2, 3))
-- Lua4-Lua5

2)table.insert / table.remove:插入、删除

local t = {1,2,3}
table.insert(t, 4)       -- 尾插
table.insert(t, 2, 99)   -- 在位置 2 插入 99

print(table.concat(t, ","))
-- 1,99,2,3,4

local v = table.remove(t, 2)  -- 删除位置 2
print(v)
-- 99
print(table.concat(t, ","))
-- 1,2,3,4

3)table.sort:排序

默认升序:

local t = {5,2,9,1}
table.sort(t)
print(table.concat(t, ","))
-- 1,2,5,9

自定义比较(降序):

table.sort(t, function(a,b)
    return a > b
end)
print(table.concat(t, ","))
-- 9,5,2,1

4)table.pack / table.unpack:打包和解包(Lua5 常用)

table.pack(...)

local t = table.pack("a", 123, "TLBB")
print(t[1], t[2], t[3], t.n)
-- a  123  TLBB  3

注意:我这里 pack 用的是 Lua4 风格存长度:t.n = 参数个数

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

local t = {"x", "y", "z"}
local a,b,c = table.unpack(t)
print(a,b,c)
-- x y z

指定范围:

local b,c = table.unpack(t, 2, 3)
print(b,c)
-- y z

常见坑(我踩过的就这几个)

  1. table.pack 返回的长度字段是 t.n
    这是 Lua4 时代常见写法。客户端服务端都不支持#t 建议脚本作者继续用 t.n 更稳。

  2. table.sort 是原地排序
    排完 t 自己就变了,不会返回新表。

  3. table.concat 只适合数组表
    里面如果混了非字符串(比如 table/function),会报错或拼不出来,建议先 tostring()


最简测试脚本(直接扔进源端跑)

print("=== table.concat ===")
local t = {"TLBB","Lua4","Lua5"}
print(table.concat(t, " | "))
print(table.concat(t, "-", 2, 3))

print("\n=== table.insert/remove ===")
local a = {1,2,3}
table.insert(a, 4)
table.insert(a, 2, 99)
print(table.concat(a, ","))
print("remove:", table.remove(a, 2))
print(table.concat(a, ","))

print("\n=== table.sort ===")
local b = {5,2,9,1}
table.sort(b)
print(table.concat(b, ","))
table.sort(b, function(x,y) return x > y end)
print(table.concat(b, ","))

print("\n=== table.pack/unpack ===")
local p = table.pack("a", 123, "TLBB")
print(p[1], p[2], p[3], p.n)
local x,y,z = table.unpack({"x","y","z"})
print(x,y,z)
您需要登录后才可以回帖 登录 | register

本版积分规则

雪舞知识库 ( 浙ICP备15015590号-1 | 萌ICP备20232229号|浙公网安备33048102000118号 ) |网站地图|天天打卡

GMT+8, 2026-1-1 05:17 , Processed in 0.088276 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表