【问题标题】:Index values from table into another table将表中的值索引到另一个表中
【发布时间】:2023-03-15 01:09:01
【问题描述】:

我想通过选择一个表的键将值存储到另一个表中,例如:

polyline = {color="blue", thickness=2, npoints=4}

stuff = {"polyline.color":[polyline.thickness]}
print(stuff)

应该产生:

blue   2

但是,我收到以下错误:

input:3: '}' 预计在 ':' 附近

【问题讨论】:

  • 试试stuff = {polyline.color, polyline.thickness}
  • 就像您在上一篇文章中一样,您正在混合 Lua 和 JavaScript 语法。除了 JavaScript 部分看起来不正确。您可能会从一次学习一种语言中受益。

标签: lua


【解决方案1】:
local polyline = {color="blue", thickness=2, npoints=4}

local stuff = {polyline.color, polyline.thickness}
print(table.unpack(stuff))

【讨论】:

    【解决方案2】:

    我相信,您正在混合一些 Python 语法。您是否注意到使用两种不同(错误)的方式来访问这些值?

    我猜,这就是你对 Lua 代码的 sn-p 的意思:

    polyline = {color = "blue", thickness = 2, npoints = 4}
    
    stuff = {[polyline.color] = polyline.thickness}
    for key, val in pairs(stuff) do
        print(key, val)
    end
    

    【讨论】:

      猜你喜欢
      • 2019-02-23
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2014-12-21
      • 2012-03-06
      • 1970-01-01
      相关资源
      最近更新 更多