将项目添加到多维数组而不覆盖旧的?
这可能是一个简单的问题,但我一直无法找到答案: 如何向数组添加值而不覆盖(所有)旧值或必须重写它们? LUA中有array_push这样的东西吗?如果是这样,它也适用于多维数组吗?
示例:
Array={"Forest","Beach","Home"} --places
Array["Forest"] = {"Trees","Flowers"} --things you find there
Array["Forest"]["Trees"] = "A tree is a perennial woody plant" --description
如果我想在新位置添加新事物的描述,我无法使用
Array["Restaurant"]["Spoon"] = "A type of cutlery."
因为我必须声明所有这些内容以及旧的内容,这样我就不会覆盖它们。所以我正在寻找类似的东西:
array_push(Array, "Restaurant")
array_push(Array["Restaurant"],"Spoon")
Array["Restaurant"]["Spoon"] = "A type of cutlery."
谢谢!
this may be a simple question, yet I haven't been able to find an answer to it:
How do I add a value to an array without overwriting (all) old values, or having to rewrite them? Is there such a thing as array_push in LUA? And if so, does it work for multidimensional arrays as well?
Example:
Array={"Forest","Beach","Home"} --places
Array["Forest"] = {"Trees","Flowers"} --things you find there
Array["Forest"]["Trees"] = "A tree is a perennial woody plant" --description
If I'd like to add a description of a new thing in a new place, I can't do it using
Array["Restaurant"]["Spoon"] = "A type of cutlery."
because I'd have to declare all these things, as well as the old ones so I don't overwrite them. So I'm looking for something like:
array_push(Array, "Restaurant")
array_push(Array["Restaurant"],"Spoon")
Array["Restaurant"]["Spoon"] = "A type of cutlery."
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面的 index 元方法实现应该可以解决问题。
请注意,您将数组索引值与字符串索引值混合在一起,我认为您无意这样做。例如,第一行将“Forest”存储在键“1”下,而第二行创建一个新的表键“Forest”,其中的表值保存顺序字符串值。下面的代码打印出生成的结构来演示我的意思。
接下来是上面列出的两个代码片段的结果输出。
有了这种理解,您就可以解决您的问题,而无需使用以下技巧。
输出结果可能就是您所期望的。
考虑到所有这些,以下内容完成了同样的事情,但在我看来更清晰。
The following index metamethod implementation should do the trick.
Note that you are mixing array indexed values with with string indexed values, and I don't think you intended to do so. For example, your first line stores "Forest" under the key "1", while the second line creates a new table key "Forest" with a table value that holds sequential string values. The following code prints out the generated structure to demonstrate my meaning.
Next is the resulting output of the two code snippets listed above.
With this understanding, you could then solve your problem without such trickery as follows.
The output is then what you probably expected.
With all of that in mind, the following accomplishes the same thing, but is much clearer in my humble opinion.
首先,你所做的根本不是一个数组,而是一个字典。尝试:
否则
table.insert
可能是您想要的array_push
First, what you're making is not an array at all, but a dictionary. Try:
Otherwise
table.insert
may be what you want inarray_push
这与标准 Lua 中几乎完全相同:
table.key 表示法等同于 table["key"] 表示法。
现在,每个项目的描述都在与数字键对应的值中,子项目作为与字符串键对应的值。
如果您确实希望具有与示例完全相同的语法,则必须使用元表(__index 和 __newindex 方法)。
This is almost identically there in standard Lua like this:
the table.key notation is equivalent of the table["key"] notation.
Now every item has it's description in the value corresponding to a number-key, and sub items as values corresponding to string keys.
If you really want to have exactly the same syntax as your example, you'll have to use metatables (__index and __newindex methods).