【问题标题】:How do I count the number of times a string appears in a table of strings in Lua?如何计算字符串出现在 Lua 中的字符串表中的次数?
【发布时间】:2017-07-11 20:28:27
【问题描述】:

我将如何计算一个字符串在表中出现的次数?

基本上我有一个表格,例如 200 个整体(但更大)...每个条目都有一个名为 name 的子条目。

所以..

itemlist[i].name == somestring.

现在我可以在遍历表时使用 if 语句轻松搜索并找到匹配项...

if string.find(string.lower(itemlist[i].name), string.lower(searchString)) ~= nil then

假设我正在搜索 Thomas,它会在找到“Thomas F Malone”时返回。

问题是在某些情况下,搜索值的结果不止一个。例如,假设有三个不同的名字都以 Thomas 开头。

目前它只会找到第一次出现的 Thomas。

所以计划是统计所有出现的Thomas,然后将它们全部输出......但我不知道如何获得在表格中找到结果的次数的数值。

TL;DR - 如何计算表中字符串的出现次数?

【问题讨论】:

    标签: lua


    【解决方案1】:

    找到匹配值后,将它们存储在临时表中,例如 table.insert(temporaryTable, value) 而不是使用return 退出函数。您可以在下面找到一个示例函数,该函数收集并计算查询字符串在多维表内的变量中出现的次数(或在操作中查看它here)。

    --data
    itemList = {
      {name = "Denny Kuhlman", id = "6688"},
      {name = "Russell Leisy", id = "3751"},
      {name = "Hilario Stermer", id = "1886"},
      {name = "Thomas Hemming", id = "9666"},
      {name = "Samuel Lafuente", id = "8232"},
      {name = "Lazaro Ashby", id = "5274"},
      {name = "Ronnie Nicosia", id = "9664"},
      {name = "Edison Seyal", id = "1344"},
      {name = "Jerald Officer", id = "9497"},
      {name = "Lupe Burdge", id = "266"},
      {name = "Stephan Iler", id = "5968"},
      {name = "Josue Stephens", id = "2128"},
      {name = "Salvador Ortmann", id = "3643"},
      {name = "Tony Ricker", id = "8799"},
      {name = "Corey Carbone", id = "6485"},
      {name = "Conrad Theberge", id = "139"},
      {name = "Arnulfo Oquendo", id = "2861"},
      {name = "Damien Balsley", id = "5572"},
      {name = "Efren Sloop", id = "7106"},
      {name = "Blair Clagon", id = "614"},
      {name = "Dario Service", id = "1411"},
      {name = "Paul Ashalintubbi", id = "3403"},
      {name = "Felix Veal", id = "1539"},
      {name = "Laurence Caskey", id = "2827"},
      {name = "Will Ranallo", id = "8463"},
      {name = "Thomas Brenner", id = "9599"},
      {name = "Claudio Hallmark", id = "6265"},
      {name = "Nolan Haslett", id = "9661"},
      {name = "Lenard Pereira", id = "5652"},
      {name = "Dusty Duer", id = "4034"},
    }
    
    --
    function countStringOccurence(query, itemList)
      query = string.lower(query)
    
      --if query string is found, store index of the itemList in table searchResult
      local searchResult = {}
      for i, item in ipairs(itemList) do
        local name = string.lower(item.name)
        if string.find(name, query) then
          table.insert(searchResult, i)
        end
      end
    
      --return both the occurence count and the list of found item
      return #searchResult, searchResult
    end
    
    --execute the function
    count, foundItemList = countStringOccurence("thomas", itemList)
    
    --print results
    print(count)                          --> 2
    
    for i, index in ipairs(foundItemList) do
      print(index, itemList[index].name)  --> 4       Thomas Hemming
                                          --> 26      Thomas Brenner
    end
    

    注意:请注意,如果您的表/列表条目可能会被移动(例如sort),则可能不建议只存储数组索引,因为在foundItemList 中收集的引用/值可能会变得不正确。

    【讨论】:

      【解决方案2】:

      一旦你找到了一些东西,就不要停止遍历表格。每次找到字符串或将结果放入表中并稍后计算其元素时,继续并增加一个计数器变量。

      local texts = {"a123", "b213", "a332", "d411", "a124"}
      local result = {}
      for i,v in ipairs(texts) do
      
        if string.find(v, "a") then
          table.insert(result, v)
        end
      
      end
      
      print(#result)
      

      【讨论】:

        猜你喜欢
        • 2012-10-03
        • 2014-04-24
        • 2010-09-21
        • 1970-01-01
        • 2020-02-21
        • 2012-02-12
        相关资源
        最近更新 更多