将项目添加到多维数组而不覆盖旧的?

发布于 2024-10-06 06:33:42 字数 755 浏览 2 评论 0原文

这可能是一个简单的问题,但我一直无法找到答案: 如何向数组添加值而不覆盖(所有)旧值或必须重写它们? 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 技术交流群。

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

发布评论

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

评论(3

以酷 2024-10-13 06:33:42

下面的 index 元方法实现应该可以解决问题。

local mt = {}

mt.__index = function(t, k)
        local v = {}
        setmetatable(v, mt)
        rawset(t, k, v)
        return v
end

Array={"Forest","Beach","Home"} --places
setmetatable(Array, mt)
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."

请注意,您将数组索引值与字符串索引值混合在一起,我认为您无意这样做。例如,第一行将“Forest”存储在键“1”下,而第二行创建一个新的表键“Forest”,其中的表值保存顺序字符串值。下面的代码打印出生成的结构来演示我的意思。

local function printtree(node, depth)
    local depth = depth or 0
    if "table" == type(node) then
        for k, v in pairs(node) do
            print(string.rep('\t', depth)..k)
            printtree(v, depth + 1)
        end
    else
        print(string.rep('\t', depth)..node)
    end
end

printtree(Array)

接下来是上面列出的两个代码片段的结果输出。

1
    Forest
2
    Beach
3
    Home
Restaurant
    Spoon
        A type of cutlery.
Forest
    1
        Trees
    2
        Flowers
    Trees
        A tree is a perennial woody plant

有了这种理解,您就可以解决您的问题,而无需使用以下技巧。

Array = {
    Forest = {},
    Beach = {},
    Home = {}
}
Array["Forest"] = {
    Trees = "",
    Flowers = "",
}
Array["Forest"]["Trees"] = "A tree is a perennial woody plant"
Array["Restaurant"] = {
    Spoon = "A type of cutlery."
}

printtree(Array)

输出结果可能就是您所期望的。

Restaurant
    Spoon
        A type of cutlery.
Beach
Home
Forest
    Flowers

    Trees
        A tree is a perennial woody plant

考虑到所有这些,以下内容完成了同样的事情,但在我看来更清晰。

Array.Forest = {}
Array.Beach = {}
Array.Home = {}

Array.Forest.Trees = ""
Array.Forest.Flowers = ""

Array.Forest.Trees = "A tree is a perennial woody plant"

Array.Restaurant = {}
Array.Restaurant.Spoon = "A type of cutlery."

printtree(Array)

The following index metamethod implementation should do the trick.

local mt = {}

mt.__index = function(t, k)
        local v = {}
        setmetatable(v, mt)
        rawset(t, k, v)
        return v
end

Array={"Forest","Beach","Home"} --places
setmetatable(Array, mt)
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."

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.

local function printtree(node, depth)
    local depth = depth or 0
    if "table" == type(node) then
        for k, v in pairs(node) do
            print(string.rep('\t', depth)..k)
            printtree(v, depth + 1)
        end
    else
        print(string.rep('\t', depth)..node)
    end
end

printtree(Array)

Next is the resulting output of the two code snippets listed above.

1
    Forest
2
    Beach
3
    Home
Restaurant
    Spoon
        A type of cutlery.
Forest
    1
        Trees
    2
        Flowers
    Trees
        A tree is a perennial woody plant

With this understanding, you could then solve your problem without such trickery as follows.

Array = {
    Forest = {},
    Beach = {},
    Home = {}
}
Array["Forest"] = {
    Trees = "",
    Flowers = "",
}
Array["Forest"]["Trees"] = "A tree is a perennial woody plant"
Array["Restaurant"] = {
    Spoon = "A type of cutlery."
}

printtree(Array)

The output is then what you probably expected.

Restaurant
    Spoon
        A type of cutlery.
Beach
Home
Forest
    Flowers

    Trees
        A tree is a perennial woody plant

With all of that in mind, the following accomplishes the same thing, but is much clearer in my humble opinion.

Array.Forest = {}
Array.Beach = {}
Array.Home = {}

Array.Forest.Trees = ""
Array.Forest.Flowers = ""

Array.Forest.Trees = "A tree is a perennial woody plant"

Array.Restaurant = {}
Array.Restaurant.Spoon = "A type of cutlery."

printtree(Array)
纵情客 2024-10-13 06:33:42

首先,你所做的根本不是一个数组,而是一个字典。尝试:

T = { Forest = { } , Beach = { } , Home = { } }
T.Forest.Spoon = "A type of cutlery"

否则 table.insert 可能是您想要的 array_push

First, what you're making is not an array at all, but a dictionary. Try:

T = { Forest = { } , Beach = { } , Home = { } }
T.Forest.Spoon = "A type of cutlery"

Otherwise table.insert may be what you want in array_push

幸福不弃 2024-10-13 06:33:42

这与标准 Lua 中几乎完全相同:

Array.Restaurant={}
Array.Restaurant.Spoon={}
Array.Restaurant.Spoon[1]="A type of cutlery."

table.key 表示法等同于 table["key"] 表示法。
现在,每个项目的描述都在与数字键对应的值中,子项目作为与字符串键对应的值。

如果您确实希望具有与示例完全相同的语法,则必须使用元表(__index 和 __newindex 方法)。

This is almost identically there in standard Lua like this:

Array.Restaurant={}
Array.Restaurant.Spoon={}
Array.Restaurant.Spoon[1]="A type of cutlery."

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).

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