Lua / mudlet, variable in trigger pattern

Can it be done, and if so, how? My googling skills have been exhausted, thanks for any help :). Ex Variable name: test Pattern to match: "blah @test blah" So obviously, if test = "cheese", "blah cheese blah" would match.

Comments

  • Apparently my post ignored the line breaks. Sorry about that...
  • Trigger: blah (\w+) blah


    if matches[2] == "cheese" then
    doSomeFunkyStuff()
    else
    doSomeOtherFunkyStuff()
    end
    Hiroma tells you, "I just got to listen to someone complain about your deadly axekick being the bane of their existence."
    Archdragon Mizik Corten, Herald of Ruin says, "Man, that was a big axk."
    Hellrazor Cain de Soulis, Sartan's Hammer says, "Your [sic] a beast."
  • That more of a workaround, which is what I've been doing. In this case I would prefer actually just matching the variable itself. It would actually be better to just re-make the trigger every time the variable changes, with the variables value hard set into the pattern. That's still a workaround though, and is poor coding IMO.
  • I'm not sure what it is you're asking for. Why would you need to remake the trigger? The whole point of a variable is that it is variable... or am I missing something?  :-/
    Hiroma tells you, "I just got to listen to someone complain about your deadly axekick being the bane of their existence."
    Archdragon Mizik Corten, Herald of Ruin says, "Man, that was a big axk."
    Hellrazor Cain de Soulis, Sartan's Hammer says, "Your [sic] a beast."
  • I don't want the trigger to "trigger" for every possible match, then use if statements after the fact.  I only want the trigger to fire if it actually matches the variable.  There is a big difference, time wise.

    In zMud, I could use #Trigger trig_highlight_target {@target} {#co ....}, and that was fine.  I didn't have to make a trigger that fired on every single word sent from the mud and then use an #if to see if it matched @target.

    Big difference there.
  • It's not possible to include a variable in a pattern. AFAIK, allowing Mudlet to be capable of this would make trigger matching as a whole less efficient.

    So you still have to decide between a wildcard trigger and checking it against a variable afterwards, or a temp trigger that is re-created any time your variable changes.
  • Maybe what you wish is:

    string.find(matches[1], "<string>")

    Which will match the string with the whole trigger match (and not only the capture). Exemple.

    trigger: Royal with cheese

    function isMatched(str)
    if string.find(matches[1], str) ~= nil then return true else return false end
    end

    isMatched("cheese") -> true

    With your exemple, you maybe need to test an array of value:

    trigger: The mouse eats the cheese

    testArray={"cheese","cat","jester"}

    function isMatchedArray(arrayToTest) 
    for i=1,#arrayToTest do 
    if isMatched(arrayToTest[i]) then return true end end
    end

    isMatchedArray(testArray) -> true (but also true with The mouse eats the cat and The mouse eats the jester)

    You can even combine with patterns. Exemple:

    isMatched("[0-9] cheeses?") will return true with The mouse eats 1 cheese and The mouse eats 5 cheeses
    image
  • Both choices will work, however I think a temp trigger will be far more efficient, even if sloppy.  Thanks peeps.

    As a side note, can you group things in a ? in regex pattern matching?  As in, an all or nothing "might appear, might not" phrase?

    For instance, alias "fs" (for farsee) is ^fs\s(\S).  To make it work with either "fs" alone, or "fs Sarapis", I made it ^fs\s?(\S)?.  However, this has the unintentional side-effect of allowing "fsh" actually trigger the alias.  This is messing up several of my short aliases (alias 't' for example).  What I'd like to do is put the ? attachment on the \s(\S) all together in one "block".  Can this be done?
  • edited September 2013
    For that kind of aliases, I usually use:

    ^fs(?: (\w+))?$

    The (\w+) here captures the argument, if you use one. The (?: )? around it makes it, plus a leading space, optional, while ensuring that this outer block isn't also captured by use of the ?: modifier.

    Or, if you prefer your notation with \s and \S:
    ^fs(?:\s(\S+))?$
  • Fantastic, thanks Iocun.  I started using \S because I incorrectly thought \w+ included white space (which it doesn't).  \w+ works fine =).  So do the ?: brackets.  Thanks!
  • edited September 2013

  • You probably didn't put in a proper if statement for the alias.

    if matches[2] ~= nil then
          send("farsee "..matches[2],false)
    else
           send("farsee "..target,false)
    end
  • edited September 2013
    You must have misspelled something then, because that's literally the farsee alias I've been using for a very long time.

    Also, as code for this trigger, I use:

    send("farsee " .. (matches[2] or target))


    Much easier than an if construct.

  • edited September 2013

  • If you really want to do it, you can, one of the ways is:

    image

    But directly putting a variable into a trigger pattern indeed is not efficient and not worth the tradeoff in convenience.
  • Ah, nice. 
  • NizarisNizaris The Holy City of Mhaldor
    edited September 2013
    @Shecks:

    There are better ways of doing this, as have been noted above, but, you should try this one.

    test = "cheese"

    Alias regex: ^blah (\w+) blah$
    f = loadstring("return " .. matches[2])
    echo(f())

    Should print "cheese" if we put in "blah test blah".

    Mind you, I have a full Lua implementation on my machine. I'm not sure if loadstring() is available in the streamlined version of Lua packaged with Mudlet.
    image
  • Nizaris said:
    @Shecks:

    There are better ways of doing this, as have been noted above, but, you should try this one.

    test = cheese

    Alias regex: ^blah (\w+) blah$
    f = loadstring("return " .. matches[2])
    echo(f())

    Should print "cheese" if we put in "blah test blah".

    Mind you, I have a full Lua implementation on my machine. I'm not sure if loadstring() is available in the streamlined version of Lua packaged with Mudlet.
    If test is a global variable, I believe you could accomplish the same thing with:
    echo(_G[matches[2]])

  • NizarisNizaris The Holy City of Mhaldor
    Eld said:
    Nizaris said:
    @Shecks:

    There are better ways of doing this, as have been noted above, but, you should try this one.

    test = cheese

    Alias regex: ^blah (\w+) blah$
    f = loadstring("return " .. matches[2])
    echo(f())

    Should print "cheese" if we put in "blah test blah".

    Mind you, I have a full Lua implementation on my machine. I'm not sure if loadstring() is available in the streamlined version of Lua packaged with Mudlet.
    If test is a global variable, I believe you could accomplish the same thing with:
    echo(_G[matches[2]])

    Ah, cool. There's a table with all global variables.
    image
  • Nizaris said:
    Mind you, I have a full Lua implementation on my machine. I'm not sure if loadstring() is available in the streamlined version of Lua packaged with Mudlet.
    Lua available in Mudlet is not cut down in any manner.
  • NizarisNizaris The Holy City of Mhaldor
    Vadimuses said:
    Nizaris said:
    Mind you, I have a full Lua implementation on my machine. I'm not sure if loadstring() is available in the streamlined version of Lua packaged with Mudlet.
    Lua available in Mudlet is not cut down in any manner.
    Ah, I was having difficulty with the socket library once. Though, I think that's part of the extended libraries, not the core implementation.
    image
  • Lua's philosophy is "batteries not included" - there is no socket library in the core Lua, and in fact we've added a couple of useful ones to it.
Sign In or Register to comment.