2017-02-22 52 views
1

我該如何計算表中字符串出現的次數?我如何計算一個字符串出現在Lua中的字符串表中的次數?

基本上,我有一個表,就像說200個樣例(但它更大)...每個條目都有一個名爲name的子條目。

所以..

itemlist[i].name == somestring. 

現在我可以搜索並找到一個匹配相當容易使用,如果statment而循環雖然表...

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

所以說,我在尋找托馬斯,當它找到「托馬斯F馬龍」時它會回來。

事情是有些情況下,搜索值有多個結果..例如..說有三個不同的名稱,都是以Thomas開頭的。

目前它只會找到第一次出現的托馬斯。

因此,計劃是統計托馬斯的所有事件,然後輸出他們所有的人......但我無法弄清楚如何獲得在表中找到結果的次數。

TL; DR - 如何計算表中字符串的出現次數?

回答

0

只要你發現了一些東西,不要停止迭代表格。繼續進行,每次找到字符串時將計數器變量遞增,或將結果放入表中並在稍後對其元素進行計數。

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) 
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收集的引用/值可能會不正確。

相關問題