|
Yanuth Newbie
Joined: 22 Feb 2006 Posts: 9
|
Posted: Wed Feb 22, 2006 10:59 am
multi-parsing? I don't know how to name the problem [:oops:] |
I am trying to parse the following line(s):
magic missile (x25)
To then grab the number and put it in a variable called 'magicmissile'... this I can manage...
But sometimes the line contains several of the same instances, like for instance:
magic missile (x25) fire missile (x10) ice missile (x35)
And then using the pattern (%1) ~(x(%d)~) will only read the 35 at the end and think %1 is
'magic missile (x25) fire missile (x10) ice missile' as a whole...
I would like to create one generic trigger that will capture the name and the numbers, no matter how often it appears on a single line...
It always has that look to it, but it can be once, twice or more times that it features on a single line
(Oh, the names used there are fictional :P sometimes they are 1,2,3,etc word names...)
I hope this is understandable, I felt a bit at a loss trying to make the trigger, but even more to describe the problem when I thought of posting about it and asking help here ;) |
|
|
|
JQuilici Adept
Joined: 21 Sep 2005 Posts: 250 Location: Austin, TX
|
Posted: Wed Feb 22, 2006 4:53 pm |
You'll need to adapt this slightly to work with your trigger, but here's something fairly close. Basically, you use %subregex to turn your line into a stringlist (splitting at the parenthesized numbers), then use %match inside a #FORALL loop to parse each of the individual elements.
Code: |
#CLASS ParseSpells {setdef}
#AL ParseSpellLine {
#VAR spellName {}
#VAR spellCount {}
#FORALL %subregex(%-1,"\) +",")|") {
#IF (%match(%i,"(*) [(]x(%n)[)]",spellName,spellCount)) {
#NOOP -- Do something interesting. I'll just echo stuff
#SAY Captured spell ~'@spellName~' with count @spellCount
}
}
}
#CLASS 0 |
Sample runs:
Code: |
> ParseSpellLine magic missile (x25) fire missile (x10) ice missile (x35)
Captured spell 'magic missile' with count 25
Captured spell 'fire missile' with count 10
Captured spell 'ice missile' with count 35
> ParseSpellLine lightning bolt (x3)
Captured spell 'lightning bolt' with count 3
|
There is one problem I can think of - if it doesn't print the (x1) when there's only one copy, we would require a whole new approach. |
|
_________________ Come visit Mozart Mud...and tell an imm that Aerith sent you! |
|
|
|
Yanuth Newbie
Joined: 22 Feb 2006 Posts: 9
|
Posted: Wed Feb 22, 2006 6:17 pm |
Thank you SO much for this :) I doubt I would have ever found that :P
And it does indeed list (x1) if there is only one copy, so that will not generate a problem...
Now I just hope the script will run fast enough that when a buffer of text is received with several of those lines in it, that it can parse them all fast enough, trial and error in that :)
|
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: Thu Feb 23, 2006 10:43 am |
Eliminate the loop and add them directly to a record variable.
#TRIGGER {^[%w ]~(x%d~)*} {#VAR TempSpells {%replace(%replace(%replace(%line," (x","="),") ","|"),")","|")};#DELITEM TempSpells {};#ADDKEY SpellsRec {@TempSpells}} |
|
_________________ The only good questions are the ones we have never answered before.
Search the Forums |
|
|
|
Yanuth Newbie
Joined: 22 Feb 2006 Posts: 9
|
Posted: Thu Feb 23, 2006 1:19 pm Choices choices... |
Oooh, that also works yeah, except for one small glitch in there...
The lines/messages are not always spaced the same... Which I tried to post in the original question, but some auto-formatting undid that I noticed :P
it normally reads more along the lines of:
magic missile (x10) ---bunch of spaces inbetween--- fire missile (x37)
(I can't seem to show it right, when I add extra spaces inbetween the post preview just removes them, I hope this makes sense however)
etc :)
I could put an extra replace in there to remove all spaces, but that would remove too many (like inbetween the words rather than inbetween the sets)... Is there any simple solution for that?
Both solutions have their merit to me, the first one definately allows it to be inserted in my current trigger sets without too much alteration, but the second one (if the extra spaces can be removed) I could use to streamline it even further I suspect...
I am not sure which version is handiest to capture the current state of the spells and their amount and then afterwards compare it against a desired list and take appropriate action to up or lower the spells to reach that... |
|
|
|
JQuilici Adept
Joined: 21 Sep 2005 Posts: 250 Location: Austin, TX
|
Posted: Thu Feb 23, 2006 3:16 pm |
Off the top of my head (without testing), you could replace the #VAR statement in Vigilante's example with:
Code: |
#VAR TempSpells {%subregex(%replace(%line," (x","="),") *","|")} |
If I'm remembering the syntax correctly, that subregex should replace a paren followed by zero-or-more spaces with the separator character.
Also note that the [code] tag I used here preserves formatting (in case you need to do that in a later post) |
|
_________________ Come visit Mozart Mud...and tell an imm that Aerith sent you! |
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: Fri Feb 24, 2006 9:46 am |
Actually tweaking the replaces is all it takes. I have had occasional odd problems with %subregex failing to return in a timely fashion, so I tend to like using the older %replace when possible.
#TRIGGER {^[%w ]~(x%d~)*} {#VAR TempSpells {%replace(%replace(%line," (x","=")," ","|")};#DELITEM TempSpells {};#ADDKEY SpellsRec {%replace(%replace(@TempSpells,"|"," "),") ","|")}} |
|
_________________ The only good questions are the ones we have never answered before.
Search the Forums |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|