加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua – table.unpack()只返回第一个元素

发布时间:2020-12-15 00:26:03 所属栏目:大数据 来源:网络整理
导读:参见英文答案 Lua unpack() messing arguments1个 有人可以向我解释为什么table.unpack()只有在table.unpack()之后的函数调用中使用其他参数时才会返回第一个表元素吗? 这是一些演示代码: local a = {1,2,3,4,5}print("Test",table.unpack(a)) -- prints "
参见英文答案 > 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测试.

解决方法

table.unpack返回多个值.在这种情况下定义的行为是,如果它不是表达式列表中的最后一个,那么将丢弃除第一个返回值之外的所有值.

从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.

作为一种解决方法,您可以将其余参数附加到表中,并使表成为最后一个参数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读