我有
function foo()
return a, b
end
x, y = foo() -- x == a, y == b
z = foo() -- z == a
是否有更简单的方法将 a
和 b
(以及函数中的任何其他变量)作为数组传输到 z
中?
使用 z = {}
初始化 z
不起作用,因为它只是将其重新定义为 foo()
的第一个结果。
最佳答案
怎么样:
-- define the function
function foo()
return "one", "two"
end
-- set z to a table of the return values of foo(). The first return
-- value will be z[1], the second one z[2], and so on.
z = {foo()}
-- print it out
for k,v in pairs(z) do
print(k, v)
end
应该得到
1 one
2 two
这就是您正在寻找的东西吗?
关于arrays - 如何将函数的所有结果存储到 Lua 中的单个变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983991/