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
Evaluating an expression
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 9648
- 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:
Another way:
You should play with both these until you attain true enlightenment.
Craig Newman
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
Code: Select all
on mouseUp
put "5 + 3" into tVar
do "put" && tVar && "into xx"
answer xx
end mouseUp
Craig Newman
-
- 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
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
Hi St_Magnusser,
welcome to the forum!
Really? Sounds like you have a rather boring life otherwise.
if the value of tVar is not a number then answer "NO" ...
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:
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
welcome to the forum!
Really? Sounds like you have a rather boring life otherwise.
OK, since the LC syntax is very english like, please finish the sentence in plain english with what you want to do:St_Magnusser wrote: ↑Fri May 18, 2018 8:00 am...
if the value of tVar is not a number then answer "NO"
if the value of tVar is not a number then answer "NO" ...
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
...
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