## Evaluating an expression

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

lohill
Posts: 736
Joined: Tue Dec 08, 2009 6:37 pm
Location: San Diego, CA USA

### Evaluating an expression

If I have a variable tVar, that may or may not be a mathematical expression, how can I determine whether or not is is and if it to resolve the expression to its numeric value.
For example if tVar = '5+2 . Should I not be able to say:
If isnumber(tVar) then put value(tVar) into tVar? In my tests the isnumber is not being passed.

Thanks,
Larry

dunbarx
VIP Livecode Opensource Backer
Posts: 5991
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Evaluating an expression

Hi.

Hi.

There are a number of ways to make this happen, but you first have to understand that the string "5 + 3" is NOT a number. You might say it is a level removed from a number, and has to be evaluated first. In a button:

Code: Select all

``````on mouseUp
put "5 + 3" into tVar
if  the value of tVar is a number then answer "OK"
end mouseUp``````
Another way:

Code: Select all

``````on mouseUp
put "5 + 3" into tVar
do "put" && tVar && "into xx"
end mouseUp``````
You should play with both these until you attain true enlightenment.

Craig Newman

St_Magnusser
Posts: 1
Joined: Fri May 18, 2018 7:45 am

### Re: Evaluating an expression

Hi, this is my first post. Exciting!

My problem: I have an expression and I would like to know whether it is a number or not. Now "the value of t Var" gives me the number - if it is a number. If it is not a number then LiveCode gives an Execution Error.
In the example below, what I would like to have here is something like an additional line like

if the value of tVar is not a number then answer "NO"

on mouseUp
put "5 + 3+" into tVar
if the value of tVar is a number then answer "OK"
end mouseUp

execution error at line 3 (do: error in source expression) near "put 5 + 3+ into xx", char 1

Klaus
Posts: 10974
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Evaluating an expression

Hi St_Magnusser,

welcome to the forum!
St_Magnusser wrote:
Fri May 18, 2018 8:00 am
Hi, this is my first post. Exciting!

Really? Sounds like you have a rather boring life otherwise.
St_Magnusser wrote:
Fri May 18, 2018 8:00 am
...
if the value of tVar is not a number then answer "NO"
OK, since the LC syntax is very english like, please finish the sentence in plain english with what you want to do:
if the value of tVar is not a number then answer "NO" ...
St_Magnusser wrote:
Fri May 18, 2018 8:00 am
put "5 + 3+" into tVar
Well, that does not really resolve to a VALID value, you know why? Hint: + (the PLUS sign)

Here some great learning resources for the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

OK, what you are looking for is something like this:

Code: Select all

``````...
put "5 + 3" into tVar
if the value of tVar is a number then
ELSE
end if
...``````
See my question above, that was easy, wasn't it?
And it is most of the time in LC!

If you are not sure if, like in your example, there is actually a valid value in your expression you should use a "TRY... END TRY..." control structure to avoid errors at all.

Best

Klaus