Thread Tools Display Modes
07-23-10, 12:04 PM   #1
shuffle
A Deviate Faerie Dragon
 
shuffle's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2008
Posts: 10
string.gmatch problems

heres my function

lua Code:
  1. local lookupTable = {["g"] =  "\|cffffd700g\|r", ["s"] = "\|cffc7c7c7s\|r", ["c"] = "\|cffeda55fc\|r"}
  2. local stripTable = {["g"] =  "", ["s"] = "", ["c"] = ""}
  3.  
  4. local staMon = "12g34s56c"
  5. local staGold = string.gsub(staMon, "%a%s-", lookupTable)
  6. for gold in string.gmatch(staMon, "%d+%g") do
  7.    strGold = string.gsub(gold, "%a", stripTable)
  8.    print(strGold.." goldvar")
  9. end
  10. for silver in string.gmatch(staMon, "%g%d+[^%s]") do
  11.    strSilver = string.gsub(silver, "%a", stripTable)
  12.    print(strSilver.." silvervar")
  13. end
  14. for copper in string.gmatch(staMon, "%d+[^%c]") do
  15.    strCopper = string.gsub(copper, "%a", stripTable)
  16.    print(strCopper.." coppervar")
  17. end

my problem is line 14

no matter what I try I can't get it to match the "56c" in staMon ( line 4 )

it either matches on all 3 or it matches on nothing

any help getting this pattern match working would be greatly appreciated
__________________
Shuffle ## <Is a Jerk> ## Stormscale - US ## 80 ## Protection Paladin
  Reply With Quote
07-23-10, 12:22 PM   #2
yssaril
A Warpwood Thunder Caller
 
yssaril's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2007
Posts: 96
you know that [^] is an inverse list so that it will never match what you put in the list

why not try (%d)c that should return 56

also if you are looking for only one thing in the string why not use string.match instead of gmatch

string match returns the first thing it finds and then stops so you can get ri of your for loop

local cooper = string.match(staMon, "(%d+)c")

you can do the same thing for gold and silver too
  Reply With Quote
07-23-10, 12:47 PM   #3
Jamash
A Fallenroot Satyr
 
Jamash's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 20
Originally Posted by shuffle View Post
heres my function

my problem is line 14

no matter what I try I can't get it to match the "56c" in staMon ( line 4 )

it either matches on all 3 or it matches on nothing

any help getting this pattern match working would be greatly appreciated
Your problem is not line 14. I highly recommend that you read Programming in Lua's section on patterns.
  Reply With Quote
07-23-10, 07:58 PM   #4
shuffle
A Deviate Faerie Dragon
 
shuffle's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2008
Posts: 10
Originally Posted by yssaril View Post
you know that [^] is an inverse list so that it will never match what you put in the list

why not try (%d)c that should return 56

also if you are looking for only one thing in the string why not use string.match instead of gmatch

string match returns the first thing it finds and then stops so you can get ri of your for loop

local cooper = string.match(staMon, "(%d+)c")

you can do the same thing for gold and silver too
Thanks for this I must have missed string.match while reading. thanks for pointing me in the right direction.
__________________
Shuffle ## <Is a Jerk> ## Stormscale - US ## 80 ## Protection Paladin

Last edited by shuffle : 07-23-10 at 08:26 PM.
  Reply With Quote

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


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