如何在 TTS Lua 中迭代通过表?

所以,我有一个类似以下的表:

PlaneToolTip= {['Vickers_0']={title="Vickers F.B.5 Gunbus", text="Vickers F.B.5 Gunbus Variant", row="Vickers", image="planes_creation"},
               ['Vickers_1']={title="Vickers F.B.5 Gunbus", text="Vickers F.B.5 Gunbus Variant 1", typeguid="7a5d62", image="Vickers F.B.5 Gunbus Variant 1"},
               ['Morane_0']={title="Morane Saulnier Type N", text="Morane Saulnier Type N Variant", row="Morane", image="planes_creation"},
               ['Morane_1']={title="Morane Saulnier Type N", text="Morane Saulnier Type N Variant 1", typeguid="dbf582", image="Morane Saulnier Type N Variant 1"},
               ['Morane_2']={title="Morane Saulnier Type N", text="Morane Saulnier Type N Variant 2", typeguid="73c10f", image="Morane Saulnier Type N Variant 2"}}

你好,我使用 Lua(Tabletopsimulator),想要在表格中搜索某些内容,例如“title”。

for a, b in pairs (PlaneToolTip) do
         for b, c in pairs (PlaneToolTip [a]) do
             if c == "Morane Saulnier Type N" then
             ...
           end
        end

end

然后它应该输出所有合适的“typeguid”。像“dbf582”,“73c10f”那样。

感谢帮助 Radoan

点赞
用户2858170
用户2858170
# get_typeguids(variant)

该函数接受一个字符串 `variant` 作为参数并返回一个包含所有 `PlaneToolTip` 中 `text` 属性包含字符串 `variant` 且 `typeguid` 属性非空的元素所对应的 `typeguid` 值的列表。

```lua
function get_typeguids(variant)
      local typeguids = {}
      for _,entry in pairs(PlaneToolTip) do
        if entry.typeguid and entry.text:find(variant)  then
          table.insert(typeguids, entry.typeguid)
        end
      end

      return typeguids
end

调用 get_typeguids 函数并将返回值作为字符串输出,元素之间使用逗号分隔。

print(table.concat(get_typeguids("Morane Saulnier Type N"), ", "))

```

2021-06-08 13:36:07