Page 1 of 1
Check value statement
Posted: Thu May 30, 2013 5:06 pm
by MaxV
Hi,
I'm working on a scientific calculator, and I'm using
value like this:
If the string is not correct, like
"oops+3+3", it crashes!
How can I test if the string is a valid script for
value or for
do functions?
Re: Check value statement
Posted: Thu May 30, 2013 5:26 pm
by jmburnod
Hi MaxV,
don't work for me
You have to change it to:
Code: Select all
put THE value of "3+3" into myresult
You can test if a string is a number like that:
Code: Select all
put isNumber("oops+3+3") return false
I hope this help
Best regards
Jean-Marc
Re: Check value statement
Posted: Thu May 30, 2013 6:07 pm
by bn
Hi Max,
(salut Jean-Marc)
As Jean-Marc said.
from the dictionary:
value
Type: function
Syntax:
the value of expression
value(expression[,object])
You have two ways to invoke a in-built function in Livecode. The "normal" way with parenthesis or "the value of xxx". In the second case you have to use "the" and "of" otherwise the compiler does not know what you mean.
here are some ways to add 2 numbers.
Code: Select all
on mouseUp
put the value of "3+3" into field 1
put cr & value("3+3") after field 1
put cr & 3 + 3 after field 1
put 3 into tVar1
put 3 into tVar2
put cr & tVar1 + tVar2 after field 1
put cr & myAddingFunction(tVar1,tVar2) after field 1
put cr & myAddingFunctionWithErrorChecking(tVar1, tVar2) after field 1
put "oops" into tVar2
put cr & myAddingFunctionWithErrorChecking(tVar1, tVar2) after field 1
end mouseUp
function myAddingFunction param1, param2
return param1 + param2
end myAddingFunction
function myAddingFunctionWithErrorChecking param1, param2
if not (param1 is a number) or not(param2 is a number) then return "error, must be numbers"
return param1 + param2
end myAddingFunctionWithErrorChecking
put this into a button and make a field.
Kind regards
Bernd
Re: Check value statement
Posted: Thu May 30, 2013 6:35 pm
by jmburnod
Simple, clear, useful... Berndly
Re: Check value statement
Posted: Fri May 31, 2013 11:09 am
by MaxV
Thank you very much
jmburnod.
Now, I solved for my calculator, but is there a check or parse function that control if statement is correct?
I was used to work with Rebol language (very similar to LiveCode) and there was a
try function that you could use this way:
Code: Select all
if try (do "put 3 into b")
then
do "put 3 into b"
else
answer "WRONG CODE"
end if
Is there any similar in LiveCode?
Re: Check value statement
Posted: Fri May 31, 2013 11:21 am
by MaxV
No, I didn't resolve even for my calculator:
return false
I tried also:
but it returns false
Any suggestions?
Re: Check value statement
Posted: Fri May 31, 2013 11:28 am
by Klaus
Hi Max,
why don't you take a look into the dictionary?
It ain't really THAT bad
Sure there is a "TRY" structure in Livecode, but is is not a function as in your Rebol example.
Code: Select all
...
try
do "put 3 into b"
## Catch possible error
catch errornum
## ERROR happened!
## Now do what you need in this case
answer "WRONG CODE!"
## Exit htis handler or do whatever you need
exit HANDLER_NAME
end try
## Success, cary on with your script...
...
Best
Klaus
Re: Check value statement
Posted: Fri May 31, 2013 12:04 pm
by bn
Hi Max,
if you code
it returns TRUE
if you just put
then it returns FALSE since it is in quotes which means in Livecode that it is a string/literal.
if you put
then it returns TRUE since Livecode "sees" that it is numbers and does the right thing.
returns TRUE since you force the evaluation before passing the parameter to the function isNumber. Without evaluation first isNumber "thinks" it is a string literal because of the quotes, see above.
Kind regards
Bernd
Re: Check value statement
Posted: Fri May 31, 2013 12:47 pm
by MaxV
Klaus wrote:Hi Max,
why don't you take a look into the dictionary?
It ain't really THAT bad

...
Best
Klaus
Thank you Klaus!!! I searched
try in the dictionary without finding it, but I found it in the User Guide! Yes!
I'll publish my scientific calculator very soon!
Thank you, thank you, thank you, thank you!

Re: Check value statement
Posted: Fri May 31, 2013 12:51 pm
by Klaus
Hm, it IS in fact in my dictionary...

Re: Check value statement
Posted: Fri May 31, 2013 5:59 pm
by jacque
Also, I'd avoid the "vaule" or "do" functions here. They both have some overhead because the engine needs to load the compiler every time either one is used, and LiveCode syntax makes them unnecessary most of the time. The examples Bernd gave that don't use "value" or "do" will work fine.
There are very few cases where scripts need those functions, the engine is pretty smart.