LUA和表数据-在嵌套循环中查找数据

时间:2020-09-25 23:56:25

标签: lua nested-loops

我有下表

function str_split_line(){
# for IFS, see https://stackoverflow.com/questions/16831429/when-setting-ifs-to-split-on-newlines-why-is-it-necessary-to-include-a-backspac
IFS="
"
    declare -n lines=$2
    while read line; do
        lines+=("${line}")
    done <<< "${1}"
}

这是系列中的一个数据值。我用

scavenging = 
{
  {
    type = "Greenskin|16",
    fast_levelup = 20,        --Number of levels with 75% chance to level up after required level
    normal_levelup = 40,      --Number of levels with 50% chance to level up after fast_levelup + required level
    slow_levelup = 40,        --Number of levels with 25% chance
    drops =                   --Drops
    {
      {items = {"Linen", "Bolt of Linen", "Coarse Thread", "Feather", "Cotton"}, droprates = {60, 10, 10, 10, 2}},
    },
  }, 
}

拉取所需的数据。问题是,有没有一种简单的方法就可以得到droprates的值,而不必做一些(成对的)?例如,现在我可以使用:

function scavenge_meta(scavenge_name)
    for _, meta in pairs(scavenging) do
        if string.match(meta.type, scavenge_name) then
            return meta
        end
    end
end

这有效,然后我可以使用founditem.fast_levelup等。例如,我希望使用founditem.drops.items访问drops表,但这是行不通的,我需要做一个对(founditem。删除),然后配对(valuefound.items)/ etc。

也许有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

如果可以更改表格,请执行以下操作:

scavenging = 
{
 ["Greenskin|16"] = {
    type = "Greenskin|16",
    fast_levelup = 20,        
    normal_levelup = 40,      
    slow_levelup = 40,        
    drops =          
    {
      {items = {"Linen", "Bolt of Linen", "Coarse Thread", "Feather", "Cotton"}, droprates = {60, 10, 10, 10, 2}},
    },
  }, 
}

并直接通过索引获取数据

print(scavenging["Greenskin|16"].fast_levelup)

否则,只需像您一样进行搜索即可。

相关问题