在 Lua 中使用带有句点的表键

发布于 2024-11-17 04:23:34 字数 490 浏览 2 评论 0原文

在 Lua 中,为一个表分配一个指定的键可能会像这样:

a = { x = 4 }

...或者可能像...

a = { ['x'] = 4 }

很简单。但是,如果我在密钥中引入句点(如在域名中),则似乎不起作用。以下所有操作都会失败:

a = { "a.b.c" = 4 }
a = { a.b.c = 4 }
a = { ['a.b.c'] = 4 }
a = { ["a.b.c"] = 4 }
a = { [a.b.c] = 4 }

所有这些都会返回相同的错误:

$ ./script.lua
/usr/bin/lua: ./script.lua:49: `}' expected near `='

我在这里缺少什么?其中几个示例看起来非常简单并且应该有效(而其他示例则有明显的问题)。

In Lua, assigning a table with a specified key might go like this:

a = { x = 4 }

...or perhaps like...

a = { ['x'] = 4 }

Easy enough. However, if I introduce periods into the key (as in a domain name) nothing seems to work. All of the following fail:

a = { "a.b.c" = 4 }
a = { a.b.c = 4 }
a = { ['a.b.c'] = 4 }
a = { ["a.b.c"] = 4 }
a = { [a.b.c] = 4 }

All of these return the same error:

$ ./script.lua
/usr/bin/lua: ./script.lua:49: `}' expected near `='

What am I missing here? Several of the examples seem quite straight-forward and should work (while others have apparent problems).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画骨成沙 2024-11-24 04:23:34

在 lua 中,表元素可以是名称或表达式。引用语言参考,“Lua 中的名称(也称为标识符)可以是任何字母、数字和下划线组成的字符串,不能以数字开头。”,在此上下文中,其他所有内容都被解释为标识符。因此,abc 作为表索引被视为表达式,对其进行求值以获得实际的表索引。这可以工作,但毫无用处:

a = { b = { c = 1 } }
x = {}
x['a.b.c'] = 7

print(x['a.b.c'])

另请注意, foo.abc 等于 foo['a']['b']['c'] 并且不是 foo['abc']

In lua table element may be either a Name or an Expression. Citing language reference, "Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit.", and everything else is interpreted as an identifier in this context. Therefore, a.b.c as a table index is treated as expression, which is evaluated to get the actual table index. This would work, but would be useless:

a = { b = { c = 1 } }
x = {}
x['a.b.c'] = 7

print(x['a.b.c'])

Also note, that foo.a.b.c is equal to foo['a']['b']['c'] and not to foo['a.b.c'].

撩心不撩汉 2024-11-24 04:23:34
a = { ['a.b.c'] = 4 }
a = { ["a.b.c"] = 4 }

这两个都是有效的。

a = { [a.b.c] = 4 }

可能有效,具体取决于所使用的确切标识符。例如

b = { c = { d = "Ohai!" } } }
a = { [b.c.d] = 4 }

将是有效的。

如果您的解释器告诉您它们无效,那么您要么做错了什么,要么解释器中存在错误。然而,其他的则无效。

a = { ['a.b.c'] = 4 }
a = { ["a.b.c"] = 4 }

These two are all valid.

a = { [a.b.c] = 4 }

This could be valid, depending on the exact identifiers used. For example

b = { c = { d = "Ohai!" } } }
a = { [b.c.d] = 4 }

would be valid.

If your interpreter is telling you they are not valid, then you've either done something else wrong, or there's a bug in the interpreter. The others however would not be valid.

ら栖息 2024-11-24 04:23:34

你的脚本还有其他问题吗?

$ ./lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = { ["a.b.c"] = 4 }
> print (a["a.b.c"])
4
> print (a.a)
nil

Is there something else wrong in your script?

$ ./lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = { ["a.b.c"] = 4 }
> print (a["a.b.c"])
4
> print (a.a)
nil
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文