Thread Tools Display Modes
05-08-09, 12:14 AM   #1
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Help: string.gmatch

Working on autoinvite function for InviteHim!

Code:
function IH_AutoInvite(_,_,msg)
	local name
	if strfind(msg, "suggests inviting") then
		if IsRaidLeader() or IsRaidOfficer() or IsPartyLeader() then
			DEFAULT_CHAT_FRAME:AddMessage("is a leader")  --debug
			i = 0
			repeat
				name = string.gmatch(msg, "%a+")
				i = i + 1
				DEFAULT_CHAT_FRAME:AddMessage(name) --debug
			until i == 5
			InviteUnit(name)
		end
	end
	return false
end

local f = CreateFrame("Frame")
f:RegisterEvent("CHAT_MSG_PARTY")
ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY", IH_AutoInvite)
To clarify what's passing:
msg = <Player> suggests inviting <given name> to the party.

All vars are passing fine and it's popping my debug is a leader, but it doesn't seem to be running the repeat portion at all (name text doesn't appear in chat frame). I need to pull the fourth word (given name) from the passed msg.
  Reply With Quote
05-12-09, 11:11 PM   #2
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
42 views and NO ONE can help me on this?
  Reply With Quote
05-12-09, 11:39 PM   #3
Shirik
Blasphemer!
Premium Member
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2007
Posts: 818
You are using string.gmatch very wrong. It returns an iterator.

Did you mean string.match? I don't even know where to start correcting your code because it makes no sense to use string.gmatch like that, unfortunately.

A typical use of string.gmatch is something like

lua Code:
  1. for name in string.gmatch(msg, "%a+") do
  2.    -- Do something with name
  3. end
__________________
たしかにひとつのじだいがおわるのお
ぼくはこのめでみたよ
だけどつぎがじぶんおばんだってことわ
しりたくなかったんだ
It's my turn next.

Shakespeare liked regexes too!
/(bb|[^b]{2})/
  Reply With Quote
05-12-09, 11:53 PM   #4
BWarner
A Black Drake
 
BWarner's Avatar
Join Date: May 2008
Posts: 87
What's that about a g string? O.o
  Reply With Quote
05-13-09, 02:17 AM   #5
Animammal
A Murloc Raider
 
Animammal's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 4
As long as the format of your msg stays consistent with the only variation being what "given name" would be, and "given name" won't actually have spaces, I think what you want is something like;

Code:
name = string.match(msg, "<(%a+)>", 2)
This should return whatever word is in between <> in msg. (the 2 is where in msg to start the search so that it won't return "Player")

And I don't think you need the loop in there at all.
  Reply With Quote
05-13-09, 07:18 AM   #6
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
If you are going to use ChatFrame_AddMessageEventFilter then I don't think you need to create a frame and register an event. That said, I think you are better off creating a frame, registering an event, and setting an OnEvent script for it. ChatFrame_AddMessageEventFilter just seems like it could break your code if someone else made a bad filter.

Code:
local frame = CreateFrame('Frame')
frame:RegisterEvent('CHAT_MSG_PARTY')
frame:SetScript('OnEvent', function(_, _, message)
   local name = strmatch(message, "^%a+ suggests inviting (%a+) to the party.$")
   if name and (IsRaidLeader() or IsRaidOfficer() or IsPartyLeader()) then
      InviteUnit(name)
   end
end)
  Reply With Quote
05-13-09, 07:18 AM   #7
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Why are you registering the event "CHAT_MSG_PARTY" and then using a
ChatFrame_AddMessageEventFilter .

Anyway, I had a look at InviteHim and I guess you want to extract the name from this string
Code:
player.." suggests inviting ".. name
?

You would do that using string.match and this pattern should work.

Code:
local name = string.match(msg, "[^%s]+ suggests inviting ([^%s]+)")
Hope this helps .


edit: Vrul you have the wrong argument, message is the first arg not the third.
  Reply With Quote
05-13-09, 07:13 PM   #8
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Slakah View Post
Vrul you have the wrong argument, message is the first arg not the third.
self, event, arg1, ...
  Reply With Quote
05-13-09, 09:07 PM   #9
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Originally Posted by Shirik View Post
You are using string.gmatch very wrong. It returns an iterator.

Did you mean string.match? I don't even know where to start correcting your code because it makes no sense to use string.gmatch like that, unfortunately.

A typical use of string.gmatch is something like

lua Code:
  1. for name in string.gmatch(msg, "%a+") do
  2.    -- Do something with name
  3. end
First time I'm really playing with string.crap and I was already pretty sure I was going to be screwing it up from the get-go.

Originally Posted by Slakah View Post
Why are you registering the event "CHAT_MSG_PARTY" and then using a
ChatFrame_AddMessageEventFilter .
Cause I'm a noob and thought I needed both.

Originally Posted by Slakah View Post
Anyway, I had a look at InviteHim and I guess you want to extract the name from this string
Code:
player.." suggests inviting ".. name
?

You would do that using string.match and this pattern should work.

Code:
local name = string.match(msg, "[^%s]+ suggests inviting ([^%s]+)")
Hope this helps .
This is exactly what I'm trying to do. Though I'm not exactly sure how this particular example works, I'll give it a go.

Originally Posted by Slakah View Post
edit: Vrul you have the wrong argument, message is the first arg not the third.
Vrul is correct. It was changed.
  Reply With Quote
05-14-09, 04:37 AM   #10
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Vrul View Post
self, event, arg1, ...
sorry I'm just being dense, I really need to take a look at things twice before saying stuff :P.

Chaos inc:
"[^%s]+" is basically a pattern which means not a space, and I think (if I'm not being silly again) will work better than "%a+" as "%a+" will only work for standard letters and none of the funky ones with accents etc.

Hope this helps .

Last edited by Slakah : 05-14-09 at 04:42 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help: string.gmatch


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off