Page 1 of 1

Evaluating an expression

Posted: Sun Feb 02, 2014 1:35 am
by lohill
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

Re: Evaluating an expression

Posted: Sun Feb 02, 2014 6:02 am
by dunbarx
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"
   answer xx
end mouseUp
You should play with both these until you attain true enlightenment.

Craig Newman

Re: Evaluating an expression

Posted: Fri May 18, 2018 8:00 am
by St_Magnusser
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

Re: Evaluating an expression

Posted: Fri May 18, 2018 11:12 am
by Klaus
Hi St_Magnusser,

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

:shock:

Really? Sounds like you have a rather boring life otherwise. :D
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 
   answer "OK"
ELSE
  answer "Not OK!"
end if
...
See my question above, that was easy, wasn't it?
And it is most of the time in LC! :D

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