OR formular

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

OR formular

Post by bsouthuk » Fri Jan 15, 2010 12:35 pm

Hi - i'm having difficulties with the OR formular. Well I hope i'm using the correct formular for what I want to achieve anyway!

I want a button to be visible if certain info is entered into a field. If this certain info is not entered into the field then the button should be invisible.

I have the following script for this:

Code: Select all

if field "a" is "Daniel" then show button "button"
if field "a" is Steven" then show button "button"
if field "a" is Dave" then show button "button"
if field "a" is Clive" then show button "button"
if field "a" is Cheryl" then show button "button"
else
hide button "button"
Is there a way that I can code this in a better easier way such as:

Code: Select all


if field "a" is "Daniel" or "Steven" or "Dave" then show button "button"
else
hide button "button"
I've tried this but does not work. Can anyone help with the correct code?

Thanks

Daniel

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

Re: OR formular

Post by Klaus » Fri Jan 15, 2010 1:21 pm

Hi Daniel,

when you use IF... THEN you really have to write:

Code: Select all

...
if fld "a" = "Daniel" OR fld "a" = "William" OR fld "a" = "XXX" ...
...
Here some hints:
1. use a variable = LESS typing :)

Code: Select all

...
put fld "a" into tFa
if tFa = "Daniel" OR tFa = "William" OR...
...
2. In cases like this one could even consider to use a SWITCH structure!
Not really less typing, but more readable!
...

Code: Select all

put fld "a" into tFa
switch tFa
## Since we wnat to show the button if any of the mentioned names are in the field
## we can simply put several "CASE" statement here!
case "Daniel"
case "William"
case "Bob"
## etc... for every name you want to check
  show btn "BMyButton"
break
default
## No conditon above has matched
  hide btn "MyButton"
break
end switch
...
3. Monsieur BOOLE is your friend!
A long but one liner :)

Code: Select all

...
put fld "a" into tFa
set the visible of btn "MyButton" to (tFa = "Daniel" OR tFa = "William" OR tFa = "Bob")
...
The engine will at first evaluate everything in parenthesis which will be TRUE or FALSE.
Then the visibilty of the namely button will be set ot this value.

Get the logics?
Once you got it you can save a lot of typing since then you often can avoid IF THEN
when setting TRUE/FALSE values.

Instead of:

Code: Select all

...
## Let the user en-/disable the navigation through card with the arrow keys:
if the hilite of btn "Navigate with arrows" = true then
  set the navigationarrows to true
else
  set the navigationarrows to true
end if
... 
Use this one liner:

Code: Select all

...
set the navigationarrows to (the hilite of btn "Navigate with arrows" = true)
## Note: Prenthesis not necessary here, just to demonstrate that you could use several different BOOLE checks here...
...
Hope that helps.


Best

Klaus

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: OR formular

Post by Dixie » Fri Jan 15, 2010 1:22 pm

Daniel...

Very simply, ry this... one field, two buttons... in the script of field 1:-

Code: Select all

on returninfield
   put "John" & cr  into line 1 of var1
   put "harry" & cr  into line 2 of var1
   
   repeat for each line thisline in var1
      if  thisline is in the text of me then set the visible of button 2 to true
      if  thisline is not in the text of me then set the visible of button 2 to false
end repeat
end returninfield
Though my cat tells me that Klaus is correct with the use of the 'switch' statement...

be well

Dixie

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: OR formular

Post by bsouthuk » Fri Jan 15, 2010 2:01 pm

Thank you so much - this really has saved me lots of time!

Cheers

Daniel

massung
Posts: 93
Joined: Thu Mar 19, 2009 5:34 pm

Re: OR formular

Post by massung » Fri Jan 15, 2010 5:05 pm

Klaus gave some excellent suggestions. Just one other one that I use periodically:

Code: Select all

if fld "foo" is among the items of "jeff,bob,mark,sam,..." then
   ## ...
end if
Jeff M.

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

Re: OR formular

Post by Klaus » Fri Jan 15, 2010 8:39 pm

Hi Dixie,
Dixie wrote:Daniel...

Very simply, ry this... one field, two buttons... in the script of field 1:-

Code: Select all

on returninfield
   put "John" & cr  into line 1 of var1
   put "harry" & cr  into line 2 of var1
   
   repeat for each line thisline in var1
      if  thisline is in the text of me then set the visible of button 2 to true
      if  thisline is not in the text of me then set the visible of button 2 to false
end repeat
end returninfield
Though my cat tells me that Klaus is correct with the use of the 'switch' statement...

be well

Dixie
yes, using a "list" of the names in question is also a good example!
One could store the names in a global variable or a custom property.

If you use this handler often one could also write a little function for this:

Code: Select all

on whatever
  put fld "a" into tName
  set the visible of btn "MyButton" to checknames(tName)
end whatever

function checknames tName
  ## I use a global in this example which has to be filled before of course
  ## again a one liner with the help of Monsieur BOOLE
  global the_names
  return (lineoffset(tName,the_names) <> 0)
  ## retrurns TRUE if the name is in the list of names and FALSE otherwise :)
 end checknames

on openstack
  global the_names
  put "Daniel" & CR & "William" & CR & "Bob" into the_names
  ...
end openstack
Best

Klaus

P.S.
Regards to your cat :)
Her is on eof ours:http://www.major-k.de/cats/bruno1.jpg

Post Reply