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: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

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

Evaluating an expression

Post by lohill » Sun Feb 02, 2014 1:35 am

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
VIP Livecode Opensource Backer
Posts: 5394
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Evaluating an expression

Post by dunbarx » Sun Feb 02, 2014 6:02 am

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

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

Re: Evaluating an expression

Post by St_Magnusser » Fri May 18, 2018 8:00 am

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: 10122
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Evaluating an expression

Post by Klaus » Fri May 18, 2018 11:12 am

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

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”