Lua的table.unpack()如何仅提取并返回第一个元素?
- 内容介绍
- 文章标签
- 相关推荐
本文共计420个文字,预计阅读时间需要2分钟。
查看英文答案+Lua+table.unpack()+解释+为什么table.unpack()只会在table.unpack()之后的函数调用中使用其他参数时才返回第一个元素?+以下是一些示例代码:+local+
(注意:由于原文内容较长,这里仅提供了修改后的开头部分,剩余内容请根据要求自行简写。)
参见英文答案 > Lua unpack() messing arguments1个有人可以向我解释为什么table.unpack()只有在table.unpack()之后的函数调用中使用其他参数时才会返回第一个表元素吗?
这是一些演示代码:
local a = {1,2,3,4,5} print("Test", table.unpack(a)) -- prints "Test 1 2 3 4 5" print(table.unpack(a), "Test") -- prints "1 Test"
我不明白为什么第二行只打印1个Test.我希望它打印1 2 3 4 5测试.有人可以解释这种行为吗?我也有兴趣如何打第二次打印1 2 3 4 5测试.
从book:
Lua always adjusts the number of results from a function to the circumstances of the call. When we call a function as a statement, Lua discards all of its results. When we use a call as an expression, Lua keeps only the first result. We get all results only when the call is the last (or the only) expression in a list of expressions.
作为一种解决方法,您可以将其余参数附加到表中,并使表成为最后一个参数.
本文共计420个文字,预计阅读时间需要2分钟。
查看英文答案+Lua+table.unpack()+解释+为什么table.unpack()只会在table.unpack()之后的函数调用中使用其他参数时才返回第一个元素?+以下是一些示例代码:+local+
(注意:由于原文内容较长,这里仅提供了修改后的开头部分,剩余内容请根据要求自行简写。)
参见英文答案 > Lua unpack() messing arguments1个有人可以向我解释为什么table.unpack()只有在table.unpack()之后的函数调用中使用其他参数时才会返回第一个表元素吗?
这是一些演示代码:
local a = {1,2,3,4,5} print("Test", table.unpack(a)) -- prints "Test 1 2 3 4 5" print(table.unpack(a), "Test") -- prints "1 Test"
我不明白为什么第二行只打印1个Test.我希望它打印1 2 3 4 5测试.有人可以解释这种行为吗?我也有兴趣如何打第二次打印1 2 3 4 5测试.
从book:
Lua always adjusts the number of results from a function to the circumstances of the call. When we call a function as a statement, Lua discards all of its results. When we use a call as an expression, Lua keeps only the first result. We get all results only when the call is the last (or the only) expression in a list of expressions.
作为一种解决方法,您可以将其余参数附加到表中,并使表成为最后一个参数.

