RPTools.net

Discussion and Support

Skip to content

It is currently Sun Oct 22, 2017 5:56 pm 






Reply to topic  [ 10 posts ] 

Previous topic | Next topic 

  Print view

Author Message
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: Separating numbers and letters in a string.
PostPosted: Wed Sep 20, 2017 3:25 pm 
I'm looking for a way to extract information from a single string. In example, if I wanted to take 1d8 and extract the information (1 dice roll, with 8 combinations), how would I go about this? I found 'endsWith' and 'startsWith' for simple things (1d8), but I have no idea where to look for more complex things, such as 10d8 and 8d10, since ends/starts only gets one character, or so I assume.

Even a point into the right part of the wiki would be greatly appreciated - an example even moreso. Thanks in advance.

_________________
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

Image


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7887
Location: Bay Area
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Wed Sep 20, 2017 4:24 pm 
There's a bunch of ways to do it. The best way is probably using regex, but if the format is xdy then you can use listGet.

Code:
[H: dice = "10d6"]
[H: x = listGet(dice,0,"d")]
[H: y = listGet(dice,1,"d")]

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Wed Sep 20, 2017 5:44 pm 
I've seen 'regex' thrown around the forums, but I have no clue what that is.

As far as listGet, that's a nifty trick - will it work for other things? In example, if I just wanted a simple string that said '5d6 Fire', would I need regex to separate entire words like that?

_________________
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

Image


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7887
Location: Bay Area
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Wed Sep 20, 2017 9:01 pm 
This should probably go in to the wiki.

Code:
<!--
   (?i) - case insensative
   
(d+) - number of dice (not optional)
   d - dice expression separator, upper or lower case
   
(d+) - die sides (not optional)
   Optional:
      space* - space before modifier
      
([+-]*d+)* - roll modifier
      space
* - space after modifier
      
(w+)* - single word for damage type
      
   Example Groups
: "(3)d(6)(+2) (Fire)"
-->
[
H: regex = "(?i)(\\d+)d(\\d+) *([+-]*\\d+)* *(\\w+)*"]
[
H, while(1), code: {
   <!-- cancel input to break out of loop -->
   [H: abort(input("diceExp|3d6+2 Fire|Enter Dice Expression|TEXT"))]
   [H: id = strfind(diceExp,regex)]
   [H: valid = getFindCount(id)]
   [H, if(valid), code: {
      [H: numDice = getGroup(id,1,1)]
      [H: numSides = getGroup(id,1,2)]
      [H: dieMod = getGroup(id,1,3)]
      [H: dmgType = getGroup(id,1,4)]
      
      
[H: output = strformat("Original: %{diceExp}<br>Dice Expression: %{numDice}d%{numSides}")]
      [H, if(! json.isEmpty(dieMod)): output = json.append(output,strformat("Modifier: %{dieMod}"))]
      [H, if(! json.isEmpty(dmgType)): output = json.append(output,strformat("Damage Type: %{dmgType}"))]
   };{
      [H: output = strformat("Invalid Dice Expression: [%{diceExp}]")]
   }]
   [H: broadcast(json.toList(output,"<br>"))]
}]
 


Example Output:
Code:
Original: 3d6+2 Fire
Dice Expression: 3d6
Modifier: 2
Damage Type: Fire


Also, check out this post for more regex info: viewtopic.php?f=20&t=26129&p=259546#p259546

amsave regex dice expression

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Fri Mar 20, 2009 4:40 am
Posts: 9413
Location: Netherlands
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Thu Sep 21, 2017 1:57 am 
added example

_________________
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC

My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent and
DPI.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Thu Sep 21, 2017 3:42 pm 
Thanks for the example, and the link - looks really complex (and honestly a bit fun), so will be poking around at it. For now, I'm just packing the data separately in a json, but I'd love to eventually be able to pull this off.

A question for your listGet example, Alias - can you use listContains() in a similar way, like to find out if the sent variable -is- a dice roll?

Example:
Code:
[h: dice=macro.args]
[h: "<!------- Let's say the dice expression received is 1d8 ---->"]

[h: isDiceExpression=listContains(dice,"d")]


Would that be able to pick up the d in the expression like your example above does? I've been looking for a way to have my attack information be variable; you can enter whole numbers (like averaged numbers in 5E), or the dice expression if you'd just rather roll it, so being able to determine if the information sent was dice would be helpful.

Even then, would it be better off to just do the opposite? Use isNumber() to see if it is -not- a dice expression?

_________________
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

Image


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7887
Location: Bay Area
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Thu Sep 21, 2017 5:21 pm 
Here's a snippet of what I use in the 3.5/PF framework.

Code:
<!-- Base Weapon Damage -->
[H, if(isNumber(Damage)), code: {
   [H: Damage.value = max(1,Damage)]
   [H: damageTipOutput = json.set(damageTipOutput,"Base Damage",Damage)]
};{
   [H: Damage.value = max(1,eval(Damage))]
   [H: damageTipOutput = json.set(damageTipOutput,"Base Damage",strformat("%{Damage} = %{Damage.value}"))]
}] 


The eval will ignore any text after a dice expression and return the rolled value.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Thu Sep 21, 2017 5:52 pm 
That's exactly what I was thinking, using the 'else' from isNumber(). Thanks for that info. Final question on the theme of this thread then, if you don't mind - is there a way to get the average of a dice expression, instead of rolling it? In example, 2d6 is 7. I was considering using your listGet() as follows:

Code:
[h: dice="2d6"]
[h: x=listGet(dice,0,"d")]
[h: y=listGet(dice,1,"d")]
[h: avDice=add(floor(y/2),0.5)]

[h: total=floor(x*avDice)]


Not sure if there's an easier way or not, but that's what I came up with.

_________________
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

Image


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7887
Location: Bay Area
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Thu Sep 21, 2017 6:45 pm 
floor(y/2*x) is what I would do

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: Re: Separating numbers and letters in a string.
PostPosted: Thu Sep 21, 2017 6:58 pm 
That's smart - for some reason I just expected the floor to round where it wouldn't - I overcomplicate everything :P Haha, thanks again Alias =)

_________________
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 10 posts ] 

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 post attachments in this forum

Search for:

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 243 on Sun Nov 04, 2012 6:14 am

Users browsing this forum: No registered users and 1 guest





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Style based on Andreas08 by Andreas Viklund

Style by Elizabeth Shulman