Lua密钥名称以表语句中的数字开头

Yad*_*ood 1 lua

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

var table = {
    '123.com': 'details'
    '456.net': 'info'
}
Run Code Online (Sandbox Code Playgroud)

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

table = { '123.com' = 'info' }
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误:

[string "local"]:1: '}' expected near '='
Run Code Online (Sandbox Code Playgroud)

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

table = {}
table['123.com'] = 'info'
Run Code Online (Sandbox Code Playgroud)

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

Jas*_*run 7

使用文字表构造函数创建Lua表时,非标识符表索引应括在方括号中.例如:

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

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