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 可以使用变量作为表键吗?[卢阿]_Lua - Fatal编程技术网

Lua 可以使用变量作为表键吗?[卢阿]

Lua 可以使用变量作为表键吗?[卢阿],lua,Lua,我想要一个用户输入作为io。读取,然后我想要使用该变量作为表的键。我对编程知之甚少 这可能吗 编辑 比如说 Alpha = {} print("foo") Table1 = io.read() Table1 = tonumber print(Alpha.Table1) 谢谢大家! 是的,您需要将该变量放在方括号中: local input = io.read() -- get the value from the user print(tbl[input]) -- access key

我想要一个用户输入作为
io。读取
,然后我想要使用该变量作为表的
键。我对编程知之甚少

这可能吗

编辑

比如说

Alpha = {}

print("foo")
Table1 = io.read()

Table1 = tonumber


print(Alpha.Table1)

谢谢大家!

是的,您需要将该变量放在方括号中:

local input = io.read() -- get the value from the user
print(tbl[input]) -- access key in "tbl" based on value in "input"
试试这个:

myTable= {"a", "b", "c"}
i = tonumber(io.read())
if i >= 1 and i <= #myTable then
    print(myTable[i])
end
myTable={“a”、“b”、“c”}
i=tonumber(io.read())

如果我>=1,我向我们展示您迄今为止所做的工作。如果我输入
foo
,您的意思是使用字符串
“foo”
作为键,还是使用名为
foo
的变量的值?您已经知道
Alpha。表1
不是您想要的。它是Alpha[“Table1”]
的简写形式。当密钥是可以作为标识符的字符串时,可以使用它。