Aggregating multiple lines.

Post Reply
nullByte
Posts: 5
Joined: Fri Jul 29, 2022 4:36 am

Aggregating multiple lines.

Post by nullByte »

Still learning the basic features of Mudlet, so please forgive the really simplistic scenario. I have no LUA experience, but have some experience from my day job in a few interpreted scripting languages.

I have a unique line before the list of lines I want to aggregate following by a new line and my prompt.

Example of input:

Code: Select all

Items for sale:
Root of Something
Bits of matter
Sand
Bits of matter
Root of Something
Sand
Potion of somesuch
Bits of matter
Potion of somesuch
Sand

(1234xp)[ 10H 50M 200V ]> 
Desired (sorted) output:

Code: Select all

Items for sale:
[..3] Bits of matter
[..2] Potion of somesuch
[..2] Root of Something
[..4] Sand

(1234xp)[ 10H 50M 200V ]> 
This list is ephemeral and not a static set of items, so I don't need to store the data long term. This is just to help an old man keep up and reduce the text spam (I don't think there's a limit on the number of lines programatically). Suggestions on strategies here? I don't need specific code, and yes I've clicked "Next Page" at least 5 times per search engine. I'm a Mudlet novice and not familiar with how to do all the things. I was considering do a multiline trigger, but I don't know how many lines I will need to capture for my count in advance...

User avatar
demonnic
Posts: 814
Joined: Sat Dec 05, 2009 3:19 pm

Re: Aggregating multiple lines.

Post by demonnic »

For this, you probably want to use a trigger chain. Make a trigger to match on the first line, and set its fire length to 99, then make a trigger which matches on your prompt, and drag that onto the first one. The code for this child trigger should be setTriggerStayOpen("Name of Parent Trigger", 0), which will close the trigger chain, and then below that the reprinting of the lines in aggregate form. Then there's a third trigger which matches on perl regex ^.+$ and captures the item name, this should also be a child of the first but below the one which matches your prompt.

https://wiki.mudlet.org/w/Manual:Trigge ... ter_Chains is the info in the wiki for it, if you want some more reading. Also, feel free to ask back here or join the discord and ask for help if you need it. =)

User avatar
Vadi
Posts: 4993
Joined: Sat Mar 14, 2009 3:13 pm

Re: Aggregating multiple lines.

Post by Vadi »

http://www.youtube.com/watch?v=nwfjzRlgG9E&hd=1 might also help for a video format

nullByte
Posts: 5
Joined: Fri Jul 29, 2022 4:36 am

Re: Aggregating multiple lines.

Post by nullByte »

So doing some fiddling, I think I've got some working code to compress the lines as desired:
Code: [show] | [select all] lua
--[[
    Counts up the occurences of each unique line
    Example Outputs:
        [..3] Unique Line A
        [.42] Unique Line B
        [183] Unique Line C
]]
function aggregate_duplicates(text_stream)
    local unique_text = {}
    local unique_count = {}
    for i, line in ipairs(text_stream) do
        if unique_text[line] == nil then
            unique_text[line] = line
            unique_count[line] = 1
        else
            unique_count[line] = unique_count[line] + 1
        end
    end
    for i, line in ipairs(unique_text) do
        unique_text[i] = string.format("[.%03d] %s", unique_count[line], line)
    end
    return unique_text
end
Still trying to wrap my head around how to call this function. Do I place this in the "Scripts" section somewhere and call it in my trigger code section like below?
Code: [show] | [select all] lua
deleteLine()
substitution_lines = aggregate_duplicates(multimatches)
-- Make Pretty -> formatted_lines
cecho(formatted_lines)
What hurts my brain is trying to understand how and when I can make line substitutions, how much that hurts performance, and how do I make this extensible to other lists I might come across. Such as entering a dungeon area and there is a huge list of NPCs to sort through.

Thank you for the suggestions. They are helpful. I might be putting the chicken before the dinosaur by making the function first, but it was easier to start with something I can figure out easily.

User avatar
demonnic
Posts: 814
Joined: Sat Dec 05, 2009 3:19 pm

Re: Aggregating multiple lines.

Post by demonnic »

I would do like the video, and use a trigger gate to capture the lines as they come in and delete them, then print out the condensed version when the prompt/ending trigger fires. Rather than trying to make one multiline AND trigger that captures it all.

nullByte
Posts: 5
Joined: Fri Jul 29, 2022 4:36 am

Re: Aggregating multiple lines.

Post by nullByte »

That appears to be the most CPU efficient method. Thank you all! Feel free to steal my code.

Post Reply