【问题标题】:Adding items to a multidimensional array without overwriting the old ones?将项目添加到多维数组而不覆盖旧的?
【发布时间】:2011-05-20 12:25:44
【问题描述】:

这可能是一个简单的问题,但我无法找到答案: 如何在不覆盖(所有)旧值或不必重写它们的情况下向数组添加值? 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."

谢谢!

【问题讨论】:

    标签: arrays lua array-push


    【解决方案1】:

    下面的 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)
    

    接下来是上面列出的两个代码 sn-ps 的结果输出。

    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)
    

    【讨论】:

    • 感谢您用一个清晰​​的例子指出数组构造错误:-),我再试一次。
    【解决方案2】:

    首先,您制作的根本不是数组,而是字典。试试:

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

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

    【讨论】:

    • table.insert 确实是我想要的!
    【解决方案3】:

    这在标准 Lua 中几乎相同,如下所示:

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

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

    如果您真的想使用与示例完全相同的语法,则必须使用元表(__index 和 __newindex 方法)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多