quickly creating a list from a trigger pattern

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

quickly creating a list from a trigger pattern

Post by Caled »

I have gone over the enemy highlighter package, but that thing is really complex and I don't understand it.

(Web): Name says, "Enemies are: Kikon, Kurogane, Blahblah, soandso."

I want to take that, and add all the names to a list I keep:
skTargets={}
At the moment, I add names to that list one by one, with:

Code: Select all

function skAdd(n)
    n=tostring(n)
    table.insert(skTargets, n)
end
How can I add a whole list of names in one hit?

In zmud, I would do:
n={%replace(@n, ", ", "|")}
skTargets={%concat(@skTargets, @n)}

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

Re: quickly creating a list from a trigger pattern

Post by Thylacine »

You could do the following (most Lua stuff still applies to Mudlet):

Code: Select all

function skAdd(s)
  string.gsub(s, "(%w+)", function(str) table.insert(skTargets, str) end)
end
See here for more info on strings.

Edit: Forgot to mention, it's not strictly necessary to call tostring() on an object if all you're doing is receiving it from a trigger. It should always be a string unless explicitly converted otherwise. Still, I don't know where else you're using skAdd.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: quickly creating a list from a trigger pattern

Post by Caled »

Thanks for the help, I'll try and work that out.

For the tostring thing - I find sometimes that arguments passed to functions need that. It doesn't make sense why sometimes it is necessary and sometimes it is not, but when I am having trouble getting a function to work, I start using tonumber and tostring just in case that is the problem.

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

Re: quickly creating a list from a trigger pattern

Post by Thylacine »

It's actually pretty simple.

string.gsub will take the second parameter as a regular expression and search through the string you provided it, recording all matches. The reason (%w+) (which matches any word) works over an entire string is because it doesn't contain 'open line'/'end line' (^/$) characters.

In our case, the third argument is a function (albeit an anonymous one), so that all matches from the string are parsed to that. Now the reason we needed to nest table.insert is because it takes two parameters -- the first being the table (variable) to add onto, and the second the actual string. I used an anonymous function (which is just a temporary function that gets 'deleted' after wards) here, as it's really the only place you'll be using it, and it makes things much less complicated.

You could also have a more general function like this:

Code: Select all

function multiAdd(tbl, string1)
  string.gsub(string1, "(%w+)", function(string2) if type(tbl) == "table" then table.insert(tbl, string2) end end)
end
That could be used to add any amount of words (separated by punctuation, whitespace, etc) to any table as long as it already exists. The if just checks to see if tbl is really a table.

You can get more complicated with gsub if you want, see that link in my last post.

Post Reply