【问题标题】:Lua key name starts with digit in table statementLua 键名在表语句中以数字开头
【发布时间】:2012-04-01 13:08:08
【问题描述】:

当键名以数字开头时,在javascript中我们可以这样定义类似数组的对象:

var table = {
    '123.com': 'details'
    '456.net': 'info'
}

但是当我在 Lua5.1 中尝试这些代码时:

table = { '123.com' = 'info' }

它会抛出一个错误:

[string "local"]:1: '}' expected near '='

但这些代码在 lua 中被接受:

table = {}
table['123.com'] = 'info'

我想知道这是否是 Lua5.1 中的一个错误。还是我错过了什么?

【问题讨论】:

    标签: lua


    【解决方案1】:

    当使用文字表构造函数创建 Lua 表时,非标识符表索引应该用方括号括起来。例如:

    table = { ['123.com'] = 'info' }

    (来自:http://www.lua.org/pil/3.6.html)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2019-03-23
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多