我如何按所示顺序保留此表? [英] How can i keep this table in the shown order?
问题描述
您好,我有一个使用字符串索引的表:
Hello i have got a table, that uses string indexes:
shirt = {
["shirtwhite.png"] = "shirt_white.png",
["shirtwhite.png^[multiply:#3f3f3f"] = "shirt_white.png^[multiply:#3f3f3f",
["shirtwhite.png^[multiply:#ff0000"] = "shirt_white.png^[multiply:#ff0000",
["shirtwhite.png^[multiply:#ff7f00"] = "shirt_white.png^[multiply:#ff7f00",
["shirtwhite.png^[multiply:#ffff00"] = "shirt_white.png^[multiply:#ffff00",
["shirtwhite.png^[multiply:#00ff00"] = "shirt_white.png^[multiply:#00ff00",
["shirtwhite.png^[multiply:#0000ff"] = "shirt_white.png^[multiply:#0000ff",
["shirtwhite.png^[multiply:#9f00ff"] = "shirt_white.png^[multiply:#9f00ff",
},
这些是可编辑游戏角色皮肤的T恤纹理(具有不同颜色的颜色值).
Theese are t-shirt-textures for an editable game-character-skin (with colour-values for different colors).
代码中还有更多的theese表,用于字符皮肤的其他部分
There are some more of theese tables in the code, for other parts of the character-skin
在此代码段中加载表格后,如何保持表格的显示顺序? tzable位于文件"skins.lua"中,代码段来自另一个lua文件
how can I keep the table in it´s shown order, while it´s loaded in this code-snippet? The tzables are in a file "skins.lua" and the code-snippet is from another lua-file
character_creator = {}
character_creator.skins = dofile(minetest.get_modpath("character_creator") .. "/skins.lua")
local skins = character_creator.skins
local skins_array = {}
minetest.after(0, function()
local function associative_to_array(associative)
local array = {}
for key in pairs(associative) do
table.insert(array, key)
end
return array
end
skins_array = {
skin = associative_to_array(skins.skin),
hair = associative_to_array(skins.hair),
eyes = associative_to_array(skins.eyes),
shirt = associative_to_array(skins.shirt),
pants = associative_to_array(skins.pants),
}
end)
推荐答案
在Lua中,只有数组(正整数索引表)具有"order"(可以使用ipairs
进行迭代);哈希表(如您正在使用的哈希表)是无序的.如果要按特定顺序遍历这样的表,通常会用键创建一个数组,按所需顺序对其进行排序,然后遍历该数组以从表中提取元素.
In Lua only arrays (positive integer-indexed tables) have "order" (can be iterated using ipairs
); the hash tables (like the one you are working with) are unordered. If you want to iterate over a table like this in a specific order, you'd usually create an array with the keys, sorted them in the order you want and then iterate over that array extracting elements from your table.
还有一些组件(例如有序表)可以跟踪插入和返回如果您想要的话,结果将以相同的顺序显示.
There are also components (like ordered table) that may keep track of insertions and return results in the same order, if that's what you want.
这篇关于我如何按所示顺序保留此表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!