Page 1 of 1

Evaluating Expressions

Posted: Wed Jul 03, 2019 9:47 pm
by Lord_Ashes
I have a framework which stores a numbers of framework attributes. In a lot of cases instead of storing totals I store expressions which help identify where the total came from. For example, instead of "16" the attribute contents may be "10+4+2". As such when I am viewing these attributes I run them through an eval() to obtain the final value.

Initially this did not work because it worked for actual expressions but not for numeric values. Thus "10+4+2" would evaluate to "16" but "16" would give an evaluation error. I got around this by prefixing the contents with "0+". Thus when the contents are only a number, the number becomes an expression such as "0+16". If the contents are an expression then adding 0 to it does not change the value.

The problem that I am running into is that I have a helper macro that dumps all of the currently set attributes to the GM. This works great as long as the attributes are of one of the types mentioned above (i.e. numeric or expression). However, I also have a couple of expressions which are text. When I evaluate these using the same process as above, MapTools treats the text as a variable and (because it is not set) prompts the user for it.

Is there a way to determine if a string is text, number of expression? I know there is a isNumeric which identifies pure numeric entries but I have not found a good way to differentiate between string expressions and actual text strings (i.e. so that I can branch and not eval() them).

I would like to do something like:

Code: Select all

[r, if(isExpression(propVal)): prop + " is " + eval(propVal) ; prop + " is " + propVal ]
Is there any easy way to do this or do I need to write a custom function for this?

Re: Evaluating Expressions

Posted: Wed Jul 03, 2019 11:06 pm
by aliasmask
You can still evaluate them to get a result, you just have to make sure any evaluated variables are also in the token type so it is defined for that token. You may also have to switchToken() so it gets the right value from that token.