Page 1 of 1

Multiple Triggers Same Line

Posted: Mon Mar 26, 2018 8:26 am
by Jaren
I am trying to figure out a way to run the same trigger multiple times every time it finds the keyword on the same line.

Example text:

Code: Select all

Foods: burrito, soft-drink, burrito, soft-drink, burrito and burrito
Example trigger with match all enabled:

Code: Select all

(\, |\: |and )(\w+)

foodtype = matches[3]
if foodtype == "burrito" then
  burritocounter = burritocounter + 1
  echo("burritos  = "..burritocounter )
end
if foodtype == "soft-drink" then
  softdrinkcounter = softdrinkcounter + 1
  echo("softdrinks = "..softdrinkcounter)
end
I would like it to echo "softdrinks = 2" and "burritos = 4" But right now all it will do it echo the first item it finds on the line. Please help! Any pointers in the right direction would be most helpful.

Re: Multiple Triggers Same Line

Posted: Mon Mar 26, 2018 11:45 am
by Jor'Mox
Given what you are trying to do, I think it would be simpler to just use the trigger to detect one of your key words, then grab the text of the entire line, and have the code run through it to count all the words you care about. Like this:

Code: Select all

local food_types = {"burrito","soft-drink"}
local food_counts = {}
for w in string.gmatch(line,"[%w%-]+") do
    if table.contains(food_types,w) then
        food_counts[w] = (food_counts[w] or 0) + 1
    end
end
for k,v in pairs(food_counts) do
    echo(k .." = " .. v .. "\n")
    -- echo(string.format("%-10s = %s\n",k,v)) -- this line does the same as the one above, but allocates 10 characters for the food type (adding spaces as needed), aligned left because of the minus sign before the 10, which I think creates a cleaner look.
end

Re: Multiple Triggers Same Line

Posted: Mon Mar 26, 2018 9:32 pm
by Jaren
Thanks, I can work with that. I never thought about running the trigger once and for looping the rest.