Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 在for循环中动态创建变量_Lua_Wireshark_Wireshark Dissector - Fatal编程技术网

Lua 在for循环中动态创建变量

Lua 在for循环中动态创建变量,lua,wireshark,wireshark-dissector,Lua,Wireshark,Wireshark Dissector,可以使用for循环动态命名变量吗?例如: t = {} For i in ipairs(tablename) do t.i = something End 我的实际问题是为wireshark解析器动态创建protofields,但如果上述不可能,我怀疑protofield问题是否可能我不完全理解您的问题,但请尝试以下操作: t = {} for i in ipairs(tablename) do _G["t"][i] = tablename[i]; end 或者,如果您的

可以使用for循环动态命名变量吗?例如:

t = {} 

For i in ipairs(tablename) do
   t.i = something
End

我的实际问题是为wireshark解析器动态创建protofields,但如果上述不可能,我怀疑protofield问题是否可能

我不完全理解您的问题,但请尝试以下操作:

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]用于全局变量(至少在魔法符文中)


如果[name]返回错误,请尝试使用setglobal(name)。

只需执行
t[i]
。这将使用值
i
索引表(
t

local t = {}

for i, _ in ipairs(othertbl) do
    t[i] = something
end

(请注意,在Lua中,
foo.bar
foo[“bar”]
的缩写。还请注意字符串
“123”
与数字
123
不同)

我的示例表是一个多维表,~37个条目。我希望能够在表中循环并为每个条目创建一个变量,该变量稍后将用于创建wireshark原型字段。最后,我会得到一个变量t.1到t.37,然后可以在解析器中使用它。只要做
t
,即
t[i]
。在vanilla Lua中也没有像
setglobal
这样的函数。