Using '+' as an identifier in a string/substring

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Belias
Kobold
Posts: 7
Joined: Wed Feb 16, 2011 10:30 am

Using '+' as an identifier in a string/substring

Post by Belias »

Hi,
i have a problem concerning the usage of an if-condition, when i am trying to use the +-sign from a substring.

i have a variable which has "3W6+3" as its content. Now i am trying to break down the string to get the number of dices, what kind of dice i use and the bonus.

Code: Select all

[h: tmpstring='3W6+3']
[r,count(length(tmpstring),''),CODE:{
[h: stringval=substring(tmpstring,roll.count,roll.count+1)]
[h,if(stringval=='W'),CODE:{
[h: wuerfelanzahl=substring(tmpstring,0,roll.count)]
[h: wuerfelarts=roll.count+1]
}]
[r,if(stringval=='+'),CODE:{
[r: cpass=roll.count]
};
{}]
}]
I used this code for it and it worked out for the part that is before 'W'. If i try to use the same way to get the part between the 'W' and the '+', i am getting an error. Thats why i tried to get the actual countposition via roll.count, when the '+' is found. that should be 3 but the reply is always 1 (even if i change the string and add numbers before it for example '123W6+3'). Does anybody know why i get the wrong reply or how i am able to avoid it?

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Using '+' as an identifier in a string/substring

Post by wolph42 »

Code: Select all

<pre>
[h: tmpstring='3W6+4']
[h: id = strfind(tmpstring, "(\\d)W(\\d)\\+(\\d)")]
[r:getGroup(id, 1, 1)]
[r:getGroup(id, 1, 2)]
[r:getGroup(id, 1, 3)]
 
I guess you can figure out the rest yourself. If not let me know.

Belias
Kobold
Posts: 7
Joined: Wed Feb 16, 2011 10:30 am

Re: Using '+' as an identifier in a string/substring

Post by Belias »

Thanks a lot!

But what if somebody adds: 12W10+13? Can i get it more flexible?
Last edited by Belias on Wed Aug 13, 2014 5:59 pm, edited 1 time in total.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Using '+' as an identifier in a string/substring

Post by wolph42 »

One thing I forgot to check is whether \\d is a digit or a number. If it's the former than it won't work for e.g. 123W567+890 so check that first.

edit: its the former. Here's the correct code:

Code: Select all

<pre>
[h: tmpstring='30W60+40']
[h: id = strfind(tmpstring, "(\\d*)W(\\d*)\\+(\\d*)")]
[r:getGroup(id, 1, 1)]
[r:getGroup(id, 1, 2)]
[r:getGroup(id, 1, 3)] 

Belias
Kobold
Posts: 7
Joined: Wed Feb 16, 2011 10:30 am

Re: Using '+' as an identifier in a string/substring

Post by Belias »

Perfect, that's what i wanted to ask. You really helped me.

User avatar
mfrizzell
Dragon
Posts: 762
Joined: Sat Feb 13, 2010 2:35 am
Location: California

Re: Using '+' as an identifier in a string/substring

Post by mfrizzell »

wolph42 wrote:One thing I forgot to check is whether \\d is a digit or a number. If it's the former than it won't work for e.g. 123W567+890 so check that first.

edit: its the former. Here's the correct code:

Code: Select all

<pre>
[h: tmpstring='30W60+40']
[h: id = strfind(tmpstring, "(\\d*)W(\\d*)\\+(\\d*)")]
[r:getGroup(id, 1, 1)]
[r:getGroup(id, 1, 2)]
[r:getGroup(id, 1, 3)] 
I have a question. How are you identifying which group is which. Is it by () with the first being the first group? The () being the second group and so on?
DCI/RPGA# 7208328396 Skype ID mfrizzell77
Characters:
Strabor - Dwarf Avenger 5th Level
Tikkanan - Human Warlock 2nd Level
----------------------------------------------------
"People are more violently opposed to fur than leather because it's safer to harass rich women than motorcycle gangs."


User avatar
mfrizzell
Dragon
Posts: 762
Joined: Sat Feb 13, 2010 2:35 am
Location: California

Re: Using '+' as an identifier in a string/substring

Post by mfrizzell »

Thanks much.
DCI/RPGA# 7208328396 Skype ID mfrizzell77
Characters:
Strabor - Dwarf Avenger 5th Level
Tikkanan - Human Warlock 2nd Level
----------------------------------------------------
"People are more violently opposed to fur than leather because it's safer to harass rich women than motorcycle gangs."

Post Reply

Return to “Macros”