Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Trigger match string for a repeating list of varying length

Trigger match string for a repeating list of varying length

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Kevnuke   USA  (145 posts)  Bio
Date Thu 18 Jul 2019 11:23 AM (UTC)

Amended on Sat 20 Jul 2019 06:45 AM (UTC) by Kevnuke

Message
I'm trying to create a trigger for a plugin that captures the list of enemies for my current city and adds them to an array. cityenemies.cyrene = {}, in this case.

Here is a shortened example of what I see when I do CITY ENEMIES in Achaea.


Enemies of the City of Cyrene:
Rip, Achilles, Terrin'tuuran, Crythril, Jems, Ama-maalier, Artanis, Jack, Tesha, Cooper
Total: 10


Currently I'm testing using a world trigger with this regex pattern:
^((, )?([A-Z][-'a-z]*))*$

And it works, surprisingly matching only on the list of names and not the line before and after it. In reality the list is about 200 names long and can be much longer. I read somewhere that the limit for wildcards in triggers is 36.

So how do I turn that list of potentially hundreds of names into something like this:

cityenemies.cyrene = {"Rip", "Achilles", "Terrin'tuuran", "Crythril", "Jems", "Ama-maalier", "Artanis", "Jack", "Tesha", "Cooper"}


so that I can compare them to a string value later?

After looking at that pattern I realize that every even numbered wildcard would be ", ". How do I group the comma and the space together without parenthesis so that it always matches either both or neither before a name without capturing them?

[EDIT] Corrected a few typos.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Thu 18 Jul 2019 12:14 PM (UTC)

Amended on Thu 18 Jul 2019 12:18 PM (UTC) by Fiendish

Message
Capture the whole line together and then split on ", " afterwards using one of the functions from http://lua-users.org/wiki/SplitJoin

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #2 on Thu 18 Jul 2019 11:16 PM (UTC)
Message
Thanks Fiendish. Looks like a split function would do that. I didn't see where one was defined though or is that a method of the string library now? Should i just use gmatch with the pattern I used to match names that I already have?
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #3 on Fri 19 Jul 2019 07:42 PM (UTC)
Message
Did you read my link?

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Fri 19 Jul 2019 11:45 PM (UTC)
Message
Template:post=6079 Please see the forum thread: http://gammon.com.au/forum/?id=6079.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #5 on Sat 20 Jul 2019 05:57 AM (UTC)
Message
Near the end of that link Fiendish gave a rather short and neat function:


function string:split(pat)
   local fields = {}
   local start = 1
   self:gsub("()("..pat..")", 
      function(c,d)
         table.insert(fields,self:sub(start,c-1))
         start = c + #d
      end
   )
   table.insert(fields, self:sub(start))
   return fields
end

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #6 on Sat 20 Jul 2019 06:41 AM (UTC)
Message
oh yea I read a lot of it, just hadn't gotten that far and I wasn't sure which one was the best solution. I tried some and all but the first name had a leading space, so I wrote this:


function names_to_array (namelist)

    local t = {}
    local pattern = "[A-Z][-'a-z]*"

    for str in string.gmatch(namelist, pattern) do
        table.insert(t, str)
    end

    return t
end


I just pass in the entire matching line from the trigger like Fiendish suggested.

Thank you for the new link. I'll read that too.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


17,962 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.