Lua中如何快速初始化关联表?

发布于 2024-07-13 00:35:09 字数 294 浏览 6 评论 0原文

在Lua中,您可以通过以下方式创建一个表:

local t = { 1, 2, 3, 4, 5 }

但是,我想创建一个关联表,我必须按照以下方式进行操作:

local t = {}
t['foo'] = 1
t['bar'] = 2

以下给出了一个错误:

local t = { 'foo' = 1, 'bar' = 2 }

有没有一种方法可以类似于我的第一个代码片段?

In Lua, you can create a table the following way :

local t = { 1, 2, 3, 4, 5 }

However, I want to create an associative table, I have to do it the following way :

local t = {}
t['foo'] = 1
t['bar'] = 2

The following gives an error :

local t = { 'foo' = 1, 'bar' = 2 }

Is there a way to do it similarly to my first code snippet ?

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

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

发布评论

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

评论(3

蓦然回首 2024-07-20 00:35:09

正确的写法是或者

local t = { foo = 1, bar = 2}

,如果表中的键不是合法标识符:

local t = { ["one key"] = 1, ["another key"] = 2}

The correct way to write this is either

local t = { foo = 1, bar = 2}

Or, if the keys in your table are not legal identifiers:

local t = { ["one key"] = 1, ["another key"] = 2}
海螺姑娘 2024-07-20 00:35:09

它的效果会更好并且可以理解

local tablename = {["key"]="value",
                   ["key1"]="value",
                   ...}

我相信,如果您像这样查找结果,

tablename.key=value
<块引用>

表格作为字典

表还可用于存储未索引的信息
与数组一样,按数字或顺序排列。 这些存储类型是
有时称为字典、关联数组、散列或映射
类型。 我们将使用术语字典,其中元素对有一个键
和一个值。 键用于设置和检索关联的值
用它。 请注意,就像数组一样,我们可以使用 table[key] = value
格式将元素插入表中。 密钥不一定是数字,
它可以是一个字符串,或者就此而言,几乎任何其他 Lua 对象
(除了 nil 或 0/0)。 让我们构建一个包含一些键值的表
其中对:

<代码>>   t = { 苹果=“绿色”,橙色=“橙色”,香蕉=“黄色” } 
  >   对于成对的 k,v(t) 执行 print(k,v) end 
  苹果绿 
  橙橙 
  香蕉黄 
  

来自: http://lua-users.org/wiki/TablesTutorial

i belive it works a bit better and understandable if you look at it like this

local tablename = {["key"]="value",
                   ["key1"]="value",
                   ...}

finding a result with : tablename.key=value

Tables as dictionaries

Tables can also be used to store information which is not indexed
numerically, or sequentially, as with arrays. These storage types are
sometimes called dictionaries, associative arrays, hashes, or mapping
types. We'll use the term dictionary where an element pair has a key
and a value. The key is used to set and retrieve a value associated
with it. Note that just like arrays we can use the table[key] = value
format to insert elements into the table. A key need not be a number,
it can be a string, or for that matter, nearly any other Lua object
(except for nil or 0/0). Let's construct a table with some key-value
pairs in it:

> t = { apple="green", orange="orange", banana="yellow" }
> for k,v in pairs(t) do print(k,v) end
apple   green
orange  orange
banana  yellow

from : http://lua-users.org/wiki/TablesTutorial

昨迟人 2024-07-20 00:35:09

要初始化具有与字符串值匹配的字符串键的关联数组,您应该使用

local petFamilies = {["Bat"]="Cunning",["Bear"]="Tenacity"};

local petFamilies = {["Bat"]=["Cunning"],["Bear"]=["Tenacity"]};

To initialize associative array which has string keys matched by string values, you should use

local petFamilies = {["Bat"]="Cunning",["Bear"]="Tenacity"};

but not

local petFamilies = {["Bat"]=["Cunning"],["Bear"]=["Tenacity"]};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文