在lua表中保存字符串的索引(数组还是字典表?)

发布于 2024-12-19 15:19:47 字数 1248 浏览 2 评论 0原文

所以我很进退两难。我有一个读取特定消息的代码,例如:

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'}

所以当我得到这个表(我仍然无法制作)时,我可以为另一个函数调用同一个表,那么我想调用表。“xmsg”=数量。我希望有人明白我在问什么。

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"}

So I have quite a dilemma. I have a code that reads a certain msg, for example:

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

reads:

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

Now I'm trying to make it insert into a table. The problem I have so far is that I can't make it count the type of string and compare it in the table to add its index.

For example:

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

What I have working so far is:

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

and then I'm just lost. I don't know how to create this table; it should be local, I believe, but the main problem I have with this is that I don't want to print it in pairs, I want to call the values from 1 to #table, in the order they were inserted. That's where my pain starts.

I want something like:

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

so when I get this table (which I still can't make), I can call the same table for another function, that well I want to call table."xmsg" = quantity. I hope someone understands what I'm asking.

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

return msgs of this function :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-12-26 15:19:47
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

我想写一个长答案来配合这段代码,但我现在没有时间,抱歉。
我稍后再做。

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

I wanted to write a long answer to accompany this code, but I don't have the time right now, sorry.
I'll do it later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文