【问题标题】:Lua Nested Unpack Bug?Lua嵌套解包错误?
【发布时间】:2011-02-25 11:09:05
【问题描述】:

问题:

我正在尝试将一个数组解压成一个数组,但它只在它是最后一个解压的项目时才有效,如果它之后有任何东西,只有第一个元素被解压。以下是我正在尝试做的一个非常基本的示例。有没有更好的方法来做到这一点,或者这是我必须应对的错误?我不想使用 table.insert,因为在表的定义中添加类似 unpack 的东西似乎更具可读性。

代码:

   print ("Error 1")
   local table1 = { {1,1}, {2,2}, {3,3} }
   local table2 = { {0,0}, unpack (table1), {4,4} }
   for n,item in ipairs (table2) do print (unpack(item)) end

   print ("Good")
   table1 = { {1,1}, {2,2}, {3,3} }
   table2 = { {0,0}, unpack (table1) }
   for n,item in ipairs (table2) do print (unpack(item)) end

   print ("Error 2")
   table1 = { {1,1}, {2,2}, {3,3} }
   table2 = { {0,0}, unpack (table1), unpack (table1) }
   for n,item in ipairs (table2) do print (unpack(item)) end

输出:

Error 1
0       0
1       1 -- {2,2} & {3,3} cut off.
4       4
Good
0       0
1       1 -- All elements unpacked.
2       2
3       3
Error 2
0       0
1       1 -- {2,2} & {3,3} cut off.
1       1 -- All elements unpacked.
2       2
3       3

注意:

我正在运行 5.1 版。

【问题讨论】:

    标签: lua


    【解决方案1】:

    不是一个错误。如果调用不是最后一个,则返回多个值的函数调用将调整为第一个值。手册上说http://www.lua.org/manual/5.1/manual.html#2.5

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 1970-01-01
      • 2014-09-23
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多