Wildcards and tables

Posted by Garrion on Tue 08 Dec 2009 04:11 AM — 3 posts, 18,205 views.

#0
Hi all,

I am wanting to match a players name and see if that player is in my 'players' table. I can do this so far with a simple if statement:

  Char_Name = string.lower(wildcards[1])
  
  if players [Char_Name] then

So if 'Garrion' tells me something it will check the name 'garrion' is valid in the players table (eg. players.garrion).

But I am finding that there are way to many variables to try to match with my trigger so it only works some of the time.

The trigger and example text that sends to this function is below:

  <trigger
   enabled="y"
   match="^(.*) (tells|asks|chats|exclaims )(.*)(to you|you|and you)(\:)(.*)"
   name="tell_hit"
   script="pshow"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  </trigger>

The ghost of Garrion D'Chaser tells you: blah blah
Garrion D'Chaser tells you: blah blah
Garrion tells you: blah blah -- matches table

These are just a few of the possible matches.

So with the above example my wildcard could be:
the ghost of garrion d'chaser
garrion d'chaser
garrion

My thinking was to somehow do a string.find on my wildcard to see if any of the words are in my table. Is this even possible and if so could someone point me in the right dirrection please?

Thanks,
Garrion.
Australia Forum Administrator #1
A brute-force approach would be to, for every player, try to match.

eg.


local match = nil
local Char_Name = string.lower(wildcards[1])
for k, v in pairs (players) do
  if string.match (Char_Name, k:lower ()) then
    match = k  -- remember match
  end -- if
end -- for

if match then  -- we found it
  Note ("Warning, player " .. match .. " here.")
end -- if


This code assumes that the player names is the key of the players table. Effectively it will go through every possible player, trying to match on whatever it found in wildcard 1.

A potential problem here is if some names are part of longer names. Eg. this code would match on GarrionSan without noticing that Garrion is only part of the word.

There are workarounds for this (eg. break up the wildcard into individual words first), but this should get you started.
#2
Thanks Nick!

Exacly what I was after :)