Check value statement

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Check value statement

Post by MaxV » Thu May 30, 2013 5:06 pm

Hi,
I'm working on a scientific calculator, and I'm using value like this:

Code: Select all

put value of "3+3" into myresult
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?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Check value statement

Post by jmburnod » Thu May 30, 2013 5:26 pm

Hi MaxV,

Code: Select all

put value of "3+3" into myresult
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
Last edited by jmburnod on Thu May 30, 2013 9:50 pm, edited 1 time in total.
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Check value statement

Post by bn » Thu May 30, 2013 6:07 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Check value statement

Post by jmburnod » Thu May 30, 2013 6:35 pm

Simple, clear, useful... Berndly
https://alternatic.ch

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Check value statement

Post by MaxV » Fri May 31, 2013 11:09 am

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?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Check value statement

Post by MaxV » Fri May 31, 2013 11:21 am

No, I didn't resolve even for my calculator:

Code: Select all

isNumber("3+3")
return false

I tried also:

Code: Select all

"3+3" is a number
but it returns false
Any suggestions?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Check value statement

Post by Klaus » Fri May 31, 2013 11:28 am

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Check value statement

Post by bn » Fri May 31, 2013 12:04 pm

Hi Max,

if you code

Code: Select all

put value ("3+3") is a number
it returns TRUE

if you just put

Code: Select all

put "3+3" is a number
then it returns FALSE since it is in quotes which means in Livecode that it is a string/literal.

if you put

Code: Select all

put 3+3 is a number
then it returns TRUE since Livecode "sees" that it is numbers and does the right thing.

Code: Select all

isNumber(value("3+3"))
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

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Check value statement

Post by MaxV » Fri May 31, 2013 12:47 pm

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! :-D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Check value statement

Post by Klaus » Fri May 31, 2013 12:51 pm

Hm, it IS in fact in my dictionary... ;-)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Check value statement

Post by jacque » Fri May 31, 2013 5:59 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply