2017-02-22 23 views
1

テーブルで文字列が何回出現するかを数える方法は?Luaの文字列テーブルに文字列が現れる回数はどのように数えますか?

基本的に私は、例えば200エンティティのようなテーブルを持っていますが(もっと大きいですが...)、各エントリにはnameという名前のサブエントリがあります。

..だから

itemlist[i].name == somestring. 

今、私は試合はとても簡単検索して見つけることができればなステートメントを使用してテーブルをループしながら、...

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

は、だから私が探してると言いますトマス、それは "トーマスFマローン"を見つけるときに戻るでしょう。

場合によっては、検索値に複数の結果があります。例えば、すべてがThomasから始まる3つの異なる名前があります。

現時点では、トーマスの最初の出現を見つけるだけです。

トーマスの出現数をすべて集計して出力することを計画していますが、結果が表に何回出てくるかを数値で調べる方法はありません。

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

returnを使用して機能を終了するのではなく、 table.insert(temporaryTable, value)を入力してください。以下では、多次元テーブル内の変数にクエリ文字列の出現を集めてカウントするサンプル関数を示します(または、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 

NB:あなたのテーブル/リストのエントリは、(例えばsort)の周りに移動する可能性がある場合foundItemListに集め参照/値が不正確になる可能性があるため、それだけで、配列のインデックスを格納することをお勧めではないかもしれないことに注意してください。

関連する問題