-1

だから私はかなりのジレンマを持っています。たとえば、特定のメッセージを読み取るコードがあります。

m.content:sub(1,8) == 'Loot of ' then

読む:

01:50 Loot of a starving wolf: a dirty fur, a salad, 2 pancakes, 60 gold

今、私はそれをテーブルに挿入しようとしています。私がこれまでに抱えている問題は、文字列の種類を数えてテーブルで比較してインデックスを追加できないことです。

例えば:

t = {dirty fur="quantity of msgs that show this",insert a new msg="how many times haves appear}

私がこれまで取り組んできたことは次のとおりです。

foreach newmessage m do
m.content:sub(1,8) == 'Loot of ' then

そして、私はただ迷っています。このテーブルの作成方法がわかりません。ローカルである必要があると思いますが、これに関する主な問題は、ペアで出力したくないことです。1 から #table までの値を、挿入された順序で呼び出したいのです。そこから私の痛みが始まります。

私は次のようなものが欲しい:

table msgs = {spear='100',something='2', ovni='123'}

したがって、このテーブルを取得すると (まだ作成できません)、別の関数に対して同じテーブルを呼び出すことができます。誰かが私が求めていることを理解してくれることを願っています。

function loot()
foreach newmessage m do
        if m.type == MSG_INFO and m.content:sub(1,8) == 'Loot of ' then
        local content = (m.content:match('Loot of .-: (.+)')):token(nil,', ')
        for i,j in ipairs(content) do
       return content
         end
      end
   end
end

この関数の戻りメッセージ:

{"3 gold coins"}
{"3 gold coins"}
{"nothing"}
{"6 gold coins", "a hand axe"}
{"12 gold coins", "a hand axe"}
4

1 に答える 1

1
TEST_LOG = [[
01:50 Loot of a starving wolf: a dirty fur, a large melon, a cactus
02:20 Loot of a giant: a large melon, an axe
03:30 You are on fire! Not really, this is just a test message
04:00 Loot of a starving wolf: a dirty fur, a tooth, a bundle of hair
04:00 Loot of a starving wolf: a dirty fur, a tooth, an axe
]]

ENEMY_LOOT_COUNTS = {}
LOOT_COUNTS = {}

for line in string.gmatch(TEST_LOG, "([^\n]+)\n") do
    local time, msg = string.match(line, "(%d%d:%d%d) (.+)$")
    if msg and msg:sub(1, 8) == "Loot of " then
        local enemy_name, contents = string.match(msg, "^Loot of a ([^:]+): (.+)$")
        local enemy_t = ENEMY_LOOT_COUNTS[enemy_name]
        if not enemy_t then
            enemy_t = {}
            ENEMY_LOOT_COUNTS[enemy_name] = enemy_t
        end
        local items = {}
        for item_name in string.gmatch(contents, "an? ([^,]+)") do
            items[#items+1] = item_name
            enemy_t[item_name] = (enemy_t[item_name] or 0)+1
            LOOT_COUNTS[item_name] = (LOOT_COUNTS[item_name] or 0)+1
        end
    else
        -- you can handle other messages here if you want
    end
end

for enemy_name, loot_counts in pairs(ENEMY_LOOT_COUNTS) do
    local s = "Enemy "..enemy_name.." dropped: "
    for item_name, item_count in pairs(loot_counts) do
        s = s..item_count.."x "..item_name..", "
    end
    print(s)
end

do
    local s = "Overall: "
    for item_name, item_count in pairs(LOOT_COUNTS) do
        s = s..item_count.."x "..item_name..", "
    end
    print(s)
end

このコードに付随する長い答えを書きたかったのですが、今は時間がありません。ごめんなさい。後でやります。

于 2011-12-06T10:04:33.877 に答える