Thread Tools Display Modes
01-03-11, 06:09 PM   #1
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,953
Question gmatch and GlobalStrings.lua matching

Okay, I am trying to do the following:

Code:
for v in string.gmatch(message, GUILD_INFO_TEMPLATE) do
  print(v);
end
Where GUILD_INFO_TEMPLATE is in the globalstrings.lua file as follows:
Code:
GUILD_INFO_TEMPLATE = "Guild created %1$d-%2$d-%3$d, %4$d players, %5$d accounts";
However, if I try that I get the following error message :
invalid capture index
And is linked to the for line above.

However, if I use this instead:
Code:
for v in string.gmatch(message, "%d+") do
  print(v)
end
It works fine and extracts the numbers one at a time but for every CHAT_MSG_SYSTEM message that arrives in the system hence the attempt at just looking at the one I want.

I want to eventually add the following to the list of chats to watch for but I can't seem to figure out how to use them as wowwiki or wowprogramming don't introduce examples like this to check against.

Code:
ERR_FRIEND_OFFLINE_S = "%s has gone offline.";
ERR_FRIEND_ONLINE_SS = "|Hplayer:%s|h[%s]|h has come online.";
Any idea how I can do this or an example of this in action and working
Thanks in advance.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
01-03-11, 07:10 PM   #2
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
Not sure what you mean but I'll bite. You want to extract each number from the string but only if the message matches GUILD_INFO_TEMPLATE, right? I'm not that great at regular expressions, quite the opposite. But hey, learn by trying.

Tried this ingame:
Code:
local s = "Guild created 10-26-2008, 51 players, 9 accounts"

local pattern = string.gsub(GUILD_INFO_TEMPLATE, "%%%d+%$d", "%%d+")

if string.match(s, pattern) then
   for v in string.gmatch(s, "%d+") do
      print(v)
   end
end
which prints
Code:
10
26
2008
51
9
to chat. In your other examples %s denotes that a string is expected for formatting, but when doing pattern matching %s is used to capture spaces. So I believe you would have to construct a new pattern by using string.gsub to convert every %s to %a+.

This is a nice resource:
http://www.wowpedia.org/Pattern_matching
__________________
Oh, the simulated horror!
  Reply With Quote
01-03-11, 07:19 PM   #3
kraftman
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 63
This could also work:

Code:
local day, month, year, players, accounts = msg:match("Guild created (%d+)\-(%d+)\-(%d+), (%d+) players, (%d+) accounts")
if day then 
print(day, month, year, players, accounts)
end
will only print if it matches that exact phrase.

EDIT: missed a bracket :P

Last edited by kraftman : 01-03-11 at 07:21 PM.
  Reply With Quote
01-03-11, 09:37 PM   #4
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,953
Ah thanks Ailae, I was reading up the various sites regarding pattern matching and it all seemed like some unknown language.

It would be nice to see examples of these weird string types and what they output but I did several google searches for the error message and there were no bites but did finally fall into a site that had a similar string setup that you showed but I couldn't get it to work so maybe I missed something out somewhere so I will try yours and see how that works.

kraftman, your example would be great if I wasn't trying to utilise the globalstrings values so I didn't have to set up my own language strings. If Blizz already has the sentence I want to look at why not use it
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
01-03-11, 10:22 PM   #5
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,953
Okay, with a combination of the two, which barring the pattern itself which I couldn't fathom rofl, I have managed to get it doing what I want the way I wanted it to For those interested or similarly afflicted here is the resulting code including example printout to see the result.

Code:
local pattern = string.gsub(GUILD_INFO_TEMPLATE, "%%%d+%$d", "(%%d+)")
if string.match(message, pattern) then
    local _,_,day, month, year, members, accounts = string.find(message,pattern);
    print("Guild was created on day ", day, " of month ", month, " in the year ", year, " and has ", members, " members split across ", accounts, " accounts ");
end
Although, thinking about it, depending on how I end up displaying it or utilising the values in my addons I may end up just displaying the chat message in its entirety instead of breaking it up or as well as. So will see. I just wanted to put this information out there and thank you both for your insights. I was getting close so your replies helped show me that I was at least understanding some of the elements enough to almost have a pattern built.

The brackets I added to make the pattern is what allows the string.find to return all values that match the pattern at once instead of one at a time. The comment
Multiple return values occur when there are more than one parenthesized section within pattern. Parentheses set off each section to be returned.
on wowwiki on page http://www.wowwiki.com/API_strmatch pointed me in that direction.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » gmatch and GlobalStrings.lua matching


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