Triggers or Script

Posted by Tsunami on Thu 08 Mar 2007 07:45 PM — 5 posts, 20,694 views.

USA #0
I need to match on any of a list of about 500 words. My question is, which is the better (faster) way to do this; with a trigger like (word1|word2|word3|...word500), or with a trigger on (\w+?) that calls a Lua script which checks if the matched word is in a table of the 500 words?

-Tsunami
USA #1
It would be much simpler to send it to the script. First off, it's easier to keep track of and edit, since it can be broken into smaller chunks to make it more organized. You don't want to have to hunt through a 500 word trigger for one tiny mistake you might make. Secondly, regex is usually kind of slow as a matching system compared with a standard trigger. The fewer options you give it, the better, since it will try to match on every single bit of output it receives.

I personally would just make a table where all the words are keys and pair it with data so that I could have something like this in the script file:

wordtable = { ["word1"] = val1, ["word2"] = val2....}
function matchwords( tname, tstr, wildcards )
  if wordtable.wilcards[foo] == not nil do
    -- insert what you wanted the trigger to do here.
  end -- check for word
end -- matchwords
Australia Forum Administrator #2
I agree with Shaun. I would make a trigger that matches on a word, and then use Lua to lookup in a table. After all, table lookups are fast because they are hashed, whereas the trigger regular expression would have to try one after the other.

Quote:

if wordtable.wilcards[foo] == not nil do


This method still has the problem I mentioned before. "not nil" will evaluate to "true". Thus you would have to store true in each table item for that to work. Better (and shorter) is:


if wordtable.wildcards[1] then
  -- blah blah
end -- if


The other problem here is the tedium of setting up the table, having to give a value to each item. One method is to make a numerically-indexed table, and convert to a keyed index table, like this:


wordtable = { 
  "Lorem", "ipsum",  "dolor",  "sit",  "amet",
  "consectetur",  "adipisicing", "elit" ,
  --- and so on 
  }  -- end of wordtable 

keyed_wordtable = {}

-- do this once - turn table into keyed table

for _, v in ipairs (wordtable) do 
  keyed_wordtable [v] = true 
end

print (keyed_wordtable ["ipsum"])   --> true
print (keyed_wordtable ["occaecat"])  --> nil


The original "wordtable" is simply a list of words - however the disadvantage is you can't do a keyed lookup. The for loop iterates through that and builds a second table, this time with "true" as the value for each entry.
Australia Forum Administrator #3
Quote:

if wordtable.wildcards[1] then
-- blah blah
end -- if


Even that is wrong because it will look for an entry called "wildcards" in your table. You need:


if wordtable [wildcards[1]] then
  -- blah blah
end -- if

USA #4
Ok, thanks for the answer. The length and unwieldiness of the trigger is not a problem, since it would have been dynamically created by the script, nor is creating the table, it's essentially read in from a file.