Evaluate if field is number or string

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Evaluate if field is number or string

Post by xavram »

Is there a syntax that would tell me if a field is a number, a string expression that returns a number, or a just a regular string?

For example, the field could contain...

7
1d6+3
This is not a number

...and I'd like an expression that would return "7", eval(1d6+3), or "This is not a number".

I know that if I try "eval" on the 3rd line, it will throw an error, so that's out. IsNumber would work on the 1st, but not the second.

Any ideas?

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Evaluate if field is number or string

Post by aliasmask »

Here's a summary of something I do.

Code: Select all

[H: abort(input("expression||Enter an Expression|TEXT"))]
[H, if(json.isEmpty(expression)): value = 0; value = expression]
[H, if(! isNumber(value)): value = eval(value)]
[R: strformat("%{expression} = %{value}")]
So, as long as the expression evaluates to a number it should work fine. For example, if you enter "corn" and corn isn't defined it will ask you for the value.

Here's a snippet of an example I use with my attack macro where players can enter in their own attack string.

Code: Select all

<!-- Temp Attack -->
[H, if(json.isEmpty(tempATK)): tempATK = 0]
[H: tempATK.total = 0]
[H, if(isNumber(tempATK)), code: {
   [H, if(tempATK): attackTipOutput = json.set(attackTipOutput,"Temp Attack",tempATK)]
   [H: tempATK.total = tempATK]
   [H: tempATK.json = tempATK]
};{
   [H: tempATK.json = json.fromList(tempATK)]
   [H, foreach(atk,tempATK.json), code: {
      [H: index = roll.count +1]
      [H: atk.name = "Temp Attack "+index]
      [H, if(isNumber(atk)): atk.value = atk; atk.value = eval(atk)]
      [H, if(atk.value && isNumber(atk)): attackTipOutput = json.set(attackTipOutput,atk.name,atk)]
      [H, if(atk.value && ! isNumber(atk)): attackTipOutput = json.set(attackTipOutput,atk.name,strformat("%{atk} = %{atk.value}"))]
      [H: tempATK.total = tempATK.total + atk.value]
   }]
}] 
 

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Evaluate if field is number or string

Post by xavram »

Hmmm, that doesn't quite work for what I need.

if you enter "Corn", that's what I want to be outputted. Not another prompt for what that means...

In my "spell setup" section, there's a field for Damage. Works fine if they enter in 10 or 2d6, you get the right result broadcasted when the spell is cast. But I was thinking it would be nice if they players could use that field for non numeric stuff, so they could put in "Increases saves for 3 rounds" or something like that. There's a lot of "cast the spell, then go try to remember what that spell actually does". This was my idea as a workaround for that, without adding a new field to the json object for spells.

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Evaluate if field is number or string

Post by xavram »

Got it

Code: Select all

[H: abort(input("expression||Enter an Expression|TEXT"))]

[h : firstChar = substring(expression, 0, 1)]

[h : endResult = ""]

[h, if(isNumber(firstChar)), code : {
	[h : endResult = eval(string(expression))]
};
{
	[h : endResult = expression]
}]

[broadcast("end = " + endResult)]

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Evaluate if field is number or string

Post by aliasmask »

I think it will give an error if blank though for substring also if the first char is a number like for 0junk for an entry. If you're parsing another source it's possible stuff like that could happen.

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Evaluate if field is number or string

Post by xavram »

It should never be blank, in the full macro, it turns a blank into a "0".

And yes someone could enter in "0haha i messed up your code"...at which point I will simply say, "garbage in, garbage out". :)

Post Reply

Return to “MapTool”