Welcome to the Achaea Forums! Please be sure to read the Forum Rules.

Mudlet: match repeatedly on same line and pass matches as arguments?

FuscoFusco Member Posts: 37 ✭✭ - Stalwart
I'm wanting to make a trigger that will fire on qw and load all the names into a table. The regex for it is (\w+)\,\s and I set it to match all occurrences in a line. I then tried having it execute table.insert(myTable, matches[2]) but this only give me a table with 1 element. How do I make it so that it will run table.insert() for each match?
Tagged:

Comments

  • KlendathuKlendathu Eye of the StormMember Posts: 3,178 @@ - Legendary Achaean
    Easiest thing to do is grab matches[1], which is the entire string, then parse that into the table, rather than trying to directly parse the output:
    splitAtComma = function(str)
    myTable = {}
    str = tostring(str:gsub(",",""))
    if string.find(str," ") then
    for word in string.gmatch(str, "%a+") do
    table.insert(myTable,word)
    end
    end
    end

    Using the code above, you can have your trigger line with the following in the white box:

    splitAtComma(matches[1])

    Simples!



    Tharos, the Announcer of Delos shouts, "It's near the end of the egghunt and I still haven't figured out how to pronounce Clean-dat-hoo."
    Fusco
  • FuscoFusco Member Posts: 37 ✭✭ - Stalwart
    I'm the blacksmith that goes to the store for tongs when I can make my own. Thanks!
  • VadimusesVadimuses Member Posts: 1,122 @ - Epic Achaean
    You can also use the 'match all' option in the trigger, then all matches will be stored in the matches table.
  • JarrodJarrod Member, Seafaring Liason Posts: 3,060 @@ - Legendary Achaean
    edited September 2015
    Klendathu said:
    Easiest thing to do is grab matches[1], which is the entire string, then parse that into the table, rather than trying to directly parse the output:
    splitAtComma = function(str)
    myTable = {}
    str = tostring(str:gsub(",",""))
    if string.find(str," ") then
    for word in string.gmatch(str, "%a+") do
    table.insert(myTable,word)
    end
    end
    end

    Using the code above, you can have your trigger line with the following in the white box:

    splitAtComma(matches[1])

    Simples!

    Why wouldn't you just want to:

    myTable = string.split(matches[1], ", ")

    ? Then have a check to replace the last entry of "and <name>" with just name.
    image
    Cascades of quicksilver light streak across the firmament as the celestial voice of Ourania intones, "Oh Jarrod..."

Sign In to Comment.