|
blarg Beginner
Joined: 14 Jul 2015 Posts: 15
|
Posted: Thu May 28, 2020 9:38 pm
Looking for help parsing a variable |
I'm struggling with something that's probably relatively simple. I have a variable that contains some information about my character's group.
Example:
Code: |
Joe 100 100
Bob 82 90
puppy dog brown 50 50
puppy dog2 40 40
|
The number of lines will vary as well the number of words before the last two numbers on each line.
I'd like to combine the instances of multiple words so that my variable becomes:
Code: |
Joe 100 100
Bob 82 90
puppy_dog_brown 50 50
puppy_dog2 40 40
|
Any advice? |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4692 Location: Pensacola, FL, USA
|
Posted: Fri May 29, 2020 2:28 am |
Assuming that the variable contains carriage returns and is several lines long...
Code: |
$this=%replace(@someVar, %cr, "|")
#LOCAL $that
#FORALL $this {
#CALL %match(%i, "([%s%w]) (%d %d)", $name, $num)
#ADDITEM $that %concat(%replace($name, " ", "_"), " ", $num)
}
someVar=%replace($that, "|", %cr) |
Something like that should work, but I didn't test it. |
|
_________________ Discord: Shalimarwildcat |
|
|
|
blarg Beginner
Joined: 14 Jul 2015 Posts: 15
|
Posted: Fri May 29, 2020 12:05 pm |
Thanks, Shalimar. I've come here for help a few times and you're always spot on. Your suggestion did exactly what I was looking for. I'm still running into a problem, though. After this initial parsing, I'm attempting to assign each piece of info to separate variables, and the first creature with multiple words in the name seems to break it even with the concat.
So if my multi-line variable looks like this:
Joe 100 100
Bob 100 100
puppy dog 50 50
Larry
With your help, it now looks like this:
Joe 100 100
Bob 100 100
puppy_dog 50 50
Larry
In this example, I would like Joe assigned to a variable name1, Bob to name2, puppy_dog to name3, Larry to name4.
Similarly, the numbers after Joe should be assigned to hp1 and mv1, and the same for everyone else hp2, mv2, etc.
I'm currently doing it with a long and ugly series of #IF checks looking for pattern matches, but seeing the loop you suggested makes me think I could do it within that loop quite easily.
Thanks again and sorry about my amateur knowledge of zscript. |
|
Last edited by blarg on Fri May 29, 2020 4:17 pm; edited 1 time in total |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4692 Location: Pensacola, FL, USA
|
Posted: Fri May 29, 2020 2:12 pm |
Code: |
#LOOP %numitems($that) {
#CALL %match(%item($that, %i), "(%x) (%d) (%d)", $name, $hp, $mv)
#VAR %concat("name", %i) {$name}
#VAR %concat("hp", %i) {$hp}
#VAR %concat("mv", %i) {$mv}
} |
|
|
_________________ Discord: Shalimarwildcat |
|
|
|
blarg Beginner
Joined: 14 Jul 2015 Posts: 15
|
Posted: Fri May 29, 2020 9:03 pm |
Spot on. Thanks for the help!
|
|
|
|
|
|
|
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
|
|