【问题标题】:How to iterate through table in TTS Lua?如何遍历 TTS Lua 中的表?
【发布时间】:2021-08-25 11:29:14
【问题描述】:

所以,我有一个表格,大致如下:

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”。

感谢您的帮助 拉多安

【问题讨论】:

    标签: lua lua-table


    【解决方案1】:
    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
    
    print(table.concat(get_typeguids("Morane Saulnier Type N"), ", "))
    

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 2011-09-02
      • 2021-10-30
      • 2018-11-21
      • 1970-01-01
      • 2016-06-03
      • 2019-02-26
      • 2010-11-29
      相关资源
      最近更新 更多