在使用预定义脚本和 LUA 运行时环境测试代码时,LUA 不会采用任何形式的字符串键值。但是,如果使用数值键,LUA 将按预期使用它。当我使用带有 LUA 文件的 Tshark 来解析数据包捕获时,此规则的例外情况。这允许字符串键值语法正常工作。有什么我可能做错了吗?
我尝试创建几个具有不同变体的 .lua 脚本文件,包括:
testArray.NewItem = "value1" testArray["NewItem"] = "value1"
NewItemValue = "NewItem" testArray[NewItemValue] = "value1"
由于尝试调用 nil 值,这些都会导致 nil 值或错误,具体取决于用于检查的返回样式。
> tcpstream = {}
> stream1 = tostring(14356)
> tcpstream[stream1] = "nothing"
> print(#tcpstream)
0
> print(tcpstream[1])
nil
> tcpstream[1] = "nothing"
> print(#tcpstream)
1
> print(tcpstream[1])
nothing
tcpstream[stream1] = "nothing" 之后的 print(#tcpstream) 的输出应该显示 1 而不是零。随后的 print(tcpstream[1]) 也应该显示“无”。