是否可以使用 for 循环动态命名变量?例如:
t = {}
For i in ipairs(tablename) do
t.i = something
End
我的实际问题包括为wireshark解剖器动态创建protofields,但如果以上不可能,我怀疑protofield问题是否可能
是否可以使用 for 循环动态命名变量?例如:
t = {}
For i in ipairs(tablename) do
t.i = something
End
我的实际问题包括为wireshark解剖器动态创建protofields,但如果以上不可能,我怀疑protofield问题是否可能
做吧t[i]
。这将使用值索引 table( t
) i
。
local t = {}
for i, _ in ipairs(othertbl) do
t[i] = something
end
(注意,在 Lua 中,foo.bar
是 . 的缩写foo["bar"]
。还要注意字符串"123"
与数字不同123
)
我不完全理解您的问题,但请尝试以下操作:
t = {}
for i in ipairs(tablename) do
_G["t"][i] = tablename[i];
end
或者,如果您的意思是(我认为您的意思是)创建包含数字的变量名称:
local tablename = {"a", "b"}
for i in ipairs(tablename) do
_G["t"..i] = tablename[i];
end
所以你有“t1”,“t2”变量。
_G[name] 用于全局变量(至少在 Runes of Magic 中)。
如果 _G[name] 返回错误,请尝试 setglobal(name) 代替。