1

これは簡単な質問かもしれませんが、答えを見つけることができませんでした。古い値を(すべて)上書きしたり、書き直したりせずに、配列に値を追加するにはどうすればよいですか?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."

ありがとう!

4

3 に答える 3

3

次のインデックスメタメソッドの実装でうまくいくはずです。

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

配列のインデックス値と文字列のインデックス値を混在させていることに注意してください。そうするつもりはないと思います。たとえば、最初の行はキー「1」の下に「Forest」を格納し、2行目は連続した文字列値を保持するテーブル値を使用して新しいテーブルキー「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)

次は、上記の2つのコードスニペットの結果の出力です。

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)
于 2010-12-06T03:39:10.363 に答える
2

まず、作成しているのは配列ではなく、辞書です。試す:

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

そうでなければtable.insertあなたが望むものかもしれませんarray_push

于 2010-12-05T19:44:00.950 に答える
1

これは、次のような標準のLuaでもほぼ同じです。

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

table.key表記は、table["key"]表記と同等です。これで、すべてのアイテムの説明が数字キーに対応する値になり、サブアイテムが文字列キーに対応する値になります。

例とまったく同じ構文が本当に必要な場合は、メタテーブル(__indexメソッドと__newindexメソッド)を使用する必要があります。

于 2010-12-05T20:02:18.823 に答える