Welcome to the Achaea Forums! Please be sure to read the Forum Rules.

Lua / mudlet, variable in trigger pattern

ShecksShecks ✭✭✭ - DistinguishedMember Posts: 626 ✭✭✭ - Distinguished
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.
"Ignore the boos.  They usually come from the cheap seats."

Comments

  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    Apparently my post ignored the line breaks. Sorry about that...
    "Ignore the boos.  They usually come from the cheap seats."
  • XliXli ✭✭✭ - Distinguished Member Posts: 354 ✭✭✭ - Distinguished
    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."
  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    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.
    "Ignore the boos.  They usually come from the cheap seats."
  • XliXli ✭✭✭ - Distinguished Member Posts: 354 ✭✭✭ - Distinguished
    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."
  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    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.
    "Ignore the boos.  They usually come from the cheap seats."
  • IocunIocun Member Posts: 3,506 @@ - Legendary Achaean
    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.
  • OrzaansynOrzaansyn Member Posts: 663 @@ - Legendary Achaean
    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
  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    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?
    "Ignore the boos.  They usually come from the cheap seats."
  • IocunIocun Member Posts: 3,506 @@ - Legendary Achaean
    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+))?$
  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    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!
    "Ignore the boos.  They usually come from the cheap seats."
  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    edited September 2013

    "Ignore the boos.  They usually come from the cheap seats."
  • DragonknightDragonknight ✭✭✭✭ - Eminent MhaldorMember Posts: 271 ✭✭✭✭ - Eminent
    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
  • IocunIocun Member Posts: 3,506 @@ - Legendary Achaean
    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.

  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    edited September 2013

    "Ignore the boos.  They usually come from the cheap seats."
  • VadimusesVadimuses Member Posts: 1,119 @ - Epic Achaean
    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.
  • ShecksShecks ✭✭✭ - Distinguished Member Posts: 626 ✭✭✭ - Distinguished
    Ah, nice. 
    "Ignore the boos.  They usually come from the cheap seats."
  • NizarisNizaris ✭✭✭✭✭ - Grand Achaean The Holy City of MhaldorMember Posts: 537 ✭✭✭✭✭ - Grand Achaean
    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
  • EldEld Member Posts: 3,946 @@ - Legendary Achaean
    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 ✭✭✭✭✭ - Grand Achaean The Holy City of MhaldorMember Posts: 537 ✭✭✭✭✭ - Grand Achaean
    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
  • VadimusesVadimuses Member Posts: 1,119 @ - Epic Achaean
    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.
    Nizaris
  • NizarisNizaris ✭✭✭✭✭ - Grand Achaean The Holy City of MhaldorMember Posts: 537 ✭✭✭✭✭ - Grand Achaean
    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
  • VadimusesVadimuses Member Posts: 1,119 @ - Epic Achaean
    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 to Comment.