How to interact with table and wildcards?

Posted by Khannon on Fri 14 Jan 2022 04:51 PM — 7 posts, 22,660 views.

#0

<triggers>
  <trigger
   enabled="y"
   match="^This is wildcard (.*?) and this is another wildcard (.*?)'$"
   regexp="y"
   send_to="10"
   sequence="100"
   group="tablegroup"
   script="learn_table"
  >
  </trigger>
</triggers>

t = {
   "Small wildcard",
   "Bigger wildcard",
   "Biggest wildcard",
    }


What I would to do is check if wildcard[2] is in the table. If it is in the table then I'd like to send a message example:


Execute("gt I'm sending this message because I found <Biggest wildcard>")


I would also like to know if I don't find a wildcard in my table how to send output saying I didn't find it in my table.

I have read quite a lot of tables, but I can't quite seem to grasp how to do it. I've always tried to do something with tables and it failed miserably every time.

Thanks in advance!
Australia Forum Administrator #1
I changed your trigger slightly because you had what seemed like a stray quote at the end, and you were sending to "Execute" however with nothing in the Send box.


<triggers>
  <trigger
   enabled="y"
   group="tablegroup"
   match="^This is wildcard (.*?) and this is another wildcard (.*?)$"
   regexp="y"
   script="learn_table"
   sequence="100"
  >
  </trigger>
</triggers>



And the code in the script file would be:


t = {
   "Small wildcard",
   "Bigger wildcard",
   "Biggest wildcard",
    }


function learn_table (name, line, wildcards, styles)
  local foo = wildcards [2]
  for k, v in ipairs (t) do
    if v == foo then
      Execute ("gt I'm sending this message because I found " .. foo)
    end -- if
  end -- for  

end -- learn_table





Alternatively you can do the whole thing inside the trigger without a script file, in which case you omit the script name "learn_table" from the trigger:



<triggers>
  <trigger
   enabled="y"
   group="tablegroup"
   match="^This is wildcard (.*?) and this is another wildcard (.*?)$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

  t = {
   "Small wildcard",
   "Bigger wildcard",
   "Biggest wildcard",
    }

  local foo = "%2"
  for k, v in ipairs (t) do
    if v == foo then
      Execute ("gt I'm sending this message because I found " .. foo)
    end -- if
  end -- for  

</send>
  </trigger>
</triggers>


Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.





The code is slightly inefficient because it involves a linear scan of the table, a better way would be to use a keyed table, and then just do a lookup, eg.


<triggers>
  <trigger
   enabled="y"
   group="tablegroup"
   match="^This is wildcard (.*?) and this is another wildcard (.*?)$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

  t = {
   ["Small wildcard"] = true,
   ["Bigger wildcard"] = true,
   ["Biggest wildcard"] = true,
    }

  if t [ "%2" ] then
      Execute ("gt I'm sending this message because I found %2" )
   end -- if

</send>
  </trigger>
</triggers>

#2
Thanks for the reply, Nick!


This is all excellent. I was googling about tables for so long, all it took you was write this reply and I immediately understood what I was doing wrong!
#3
The first reason why I wanted to use the table in the script was to blacklist words/messages(in form of wildcards sort of) actually, I never figured that out.

In the reply you sent, I was able to successfully make my table work the way I intended, but in another script, I'd like to do the opposite if wildcards are in the table, prevent script shooting message/execute/action. This is something I was looking around in these forums, however, what I found was not exactly what I was looking for and I couldn't really write it correctly. If you have simple few lines to spare for me, would be wonderful.
Amended on Sat 15 Jan 2022 01:55 AM by Khannon
Australia Forum Administrator #4
Make a try and post what you come up with.
#5
Trigger to let's say an open chat/channel in-game. Someone calls someone else with a blacklisted word/phrase and I want to block the message being sent to another channel

<triggers>
  <trigger
   enabled="y"
   group="bList"
   match="^Open Chat: (.*?) called (.*?) <blacklisted word/phrase> $"
   regexp="y"
   script="bList"
   sequence="100"
  >
  </trigger>
</triggers>



Based on last time I would make a table with words/phrases that I would want to be blacklisted.


t = {
     "bad word",
     "very bad word",
     "bad phrase"
    }


I would like to utilize I think string.find in my table in the script, but I don't quite know how to write it


function bList (name, line, wildcards)

-- the parts I don't know how to write
-- if msg doesn't include blacklisted words/phrases 
-- send the Open Chat message to Global msg, if it does then -- don't forward the message to Global msg.


I have tried to utilize string.find in table, I guess I don't really know how to work with string.find and k and v.

But it also doesn't have to be string.find, it's just something I tried to mess with, but it was going nowhere at all, heh.

if I'm just trying to use string.find to match the variable, that much I've learned, I think.


 
 str = wildcards[1]
 if string.find(str, "Found string") then
 print("Found string " ..str)
Amended on Wed 19 Jan 2022 06:27 PM by Khannon
Australia Forum Administrator #6
Is the blacklisted phrase in a wildcard?

If so, can you not at least find and print that (potentially) blacklisted phrase?

If you can print it, then you have something you know you want to look up in a table. Once you get to that point it is like my last answer.