How do I break one property into two?

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
Zaran
Kobold
Posts: 7
Joined: Fri Feb 04, 2011 2:33 pm

How do I break one property into two?

Post by Zaran »

I'm working on some Heavy Gear macros and I'd like to take a property that's in this form: "xD+n" and turn it into two new properties "x" and "n"

How do I do this? (the D will always be in the property)

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: How do I break one property into two?

Post by aliasmask »

So, what are the possible combinations:
xD
xD+n
xD-n

??

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

Re: How do I break one property into two?

Post by wolph42 »

your a bit vague, but if i take it literally and assuming that x and n are always single characters then you can use Wiki: substring():

Code: Select all

[prop="xD+n"]
[x = substring(prop, 0,1)]
[n = substring(prop, 3)]
if they're not, then you'll probly need regex to retrieve them. bit trickier and you'll need: Wiki: getGroup()

Zaran
Kobold
Posts: 7
Joined: Fri Feb 04, 2011 2:33 pm

Re: How do I break one property into two?

Post by Zaran »

wolph42 wrote:your a bit vague, but if i take it literally and assuming that x and n are always single characters then you can use Wiki: substring():

Code: Select all

[prop="xD+n"]
[x = substring(prop, 0,1)]
[n = substring(prop, 3)]
if they're not, then you'll probly need regex to retrieve them. bit trickier and you'll need: Wiki: getGroup()

I think this is exactly what I need. Thanks.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: How do I break one property into two?

Post by aliasmask »

Zaran wrote:
wolph42 wrote:your a bit vague, but if i take it literally and assuming that x and n are always single characters then you can use Wiki: substring():

Code: Select all

[prop="xD+n"]
[x = substring(prop, 0,1)]
[n = substring(prop, 3)]
if they're not, then you'll probly need regex to retrieve them. bit trickier and you'll need: Wiki: getGroup()

I think this is exactly what I need. Thanks.
That will work is x and n is a single digit.

Since you didn't answer my questions I'm going to assume the format will always be xD+n. I suggest doing this:

Code: Select all

[H: prop="xD+n"]
[H: x = listGet(prop,0,"D")] <!-- everything before "D" -->
[H: n = listGet(prop,1,"+")] <!-- everything after "+" -->
But if I were to do this the "right" way, I would use regex.

User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: How do I break one property into two?

Post by Bone White »

I'd personally use listGet's list separator in this situation... Just for the sake of multiple opinions.

[code][h: diceString = "1d5"]
[r: diceQuantity = listGet(diceList,0,"d")]
[r: diceSides = listGet(diceList,1,"d")][/code]


Results in:
1
5

edited to remove the wasteful stringToList and just use listGet
Last edited by Bone White on Mon May 18, 2015 6:34 pm, edited 1 time in total.

Zaran
Kobold
Posts: 7
Joined: Fri Feb 04, 2011 2:33 pm

Re: How do I break one property into two?

Post by Zaran »

aliasmask wrote:So, what are the possible combinations:
xD
xD+n
xD-n

??
These are the possible combinations yes.

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

Re: How do I break one property into two?

Post by wolph42 »

Zaran wrote:
aliasmask wrote:So, what are the possible combinations:
xD
xD+n
xD-n

??
These are the possible combinations yes.
in that case youll need to check the string Wiki: length ()cause the second substring command will render an error in case of xD

Zaran
Kobold
Posts: 7
Joined: Fri Feb 04, 2011 2:33 pm

Re: How do I break one property into two?

Post by Zaran »

wolph42 wrote:
Zaran wrote:
aliasmask wrote:So, what are the possible combinations:
xD
xD+n
xD-n

??
These are the possible combinations yes.
in that case youll need to check the string Wiki: length ()cause the second substring command will render an error in case of xD
I can work around that by just making it xD+0 .

The whole problem is routed around mechanics of the Silhouette system. 2d+1 doesn't do 3-13 as a result in that game. It takes the highest of the dice rolled and adds 1 for every extra 6 rolled. So i'll need to be able to break down how one records each skill roll into the two parts so that it can roll the dice and then add the mod.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: How do I break one property into two?

Post by aliasmask »

I think this should cover most instances including bad input.

Code: Select all

[H: abort(input("strInput|0|Enter dice string. Format xD[+/-n]"))]
<!-- 
   (?i) = case insensitive; 
   ([0-9]+) = 1 or more digits (group 1); 
   D(.*) = D followed by anything, anything is (group2) 
-->
[H: regex = "(?i)([0-9]+)D(.*)"]
[H: findId = strfind(strInput,regex)]
[H: foundMatch = getFindCount(findId)]

[H, if(foundMatch), code: {
   <!-- x is the first match of group 1 -->
   [H: x = getGroup(findId,1,1)]
   <!-- n is the first match of group 2 -->
   [H: n = getGroup(findId,1,2)]
   <!-- clean up n. Save only digits and +,- symbols. -->
   [H, if(length(n)): n = replace(n,"[^0-9+-]","")]
   <!-- set n to 0 if not a number (like a blank) -->
   [H, if(! isNumber(n)): n = 0]
};{
   [H: x = ""]
   [H: n = ""]
}]
[R, if(foundMatch): strformat("Input: %{strInput}; x = %{x}; n = %{n}"); 
      strformat("No matches found: '%{strInput}'")] 

Post Reply

Return to “Macros”