View Single Post
04-16-11, 07:09 PM   #7
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Take a look on what I wrote and give me some feedback:

lua Code:
  1. local trim = function(str)
  2.   return str:trim(" \t\r\n")
  3. end
  4.  
  5. local match = function(str)
  6.   local ret = {}
  7.   for msg, emote in str:gmatch("(.-)%*(.-)%*") do
  8.     str = str:replace(msg, "")
  9.     str = str:replace("*"..emote.."*", "")
  10.     msg = trim(msg)
  11.     emote = trim(emote)
  12.     table.insert(ret, {
  13.       msg:len() > 0 and msg or nil,
  14.       emote:len() > 0 and emote or nil,
  15.     })
  16.   end
  17.   str = trim(str)
  18.   if str:len() > 0 then
  19.     table.insert(ret, {str, nil})
  20.   end
  21.   return #ret > 0 and ret or nil
  22. end

In short, match("message") will return either nil if nothing matched or a table containing {message, emote} that you can go trough each one, unpack each entry and send the message in the /say or /emote channels depending if it's a string or nil.

Far better than my first sketch, aye?

Last edited by Vlad : 04-16-11 at 07:34 PM.
  Reply With Quote