Multiple Conditions & "is not in"

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
deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Multiple Conditions & "is not in"

Post by deeverd » Tue Jun 16, 2015 9:15 pm

Hello LC Forum,

Although for years I've been using if-then-else statements with multiple conditions, with varying degrees of success I might add, it was only yesterday that I thought about reading the documentation for "if." It came as quite a surprise then to discover that an if-then-else should be used only with single conditions, while switch & case should be used for multiple conditions. Apparently it pays to read the dictionary.

Here's my current quandary after hours of experiments:

Multiple conditions with "case" seem to be only about what is true. But what about what's false?

In other words, I find that when I need to set a condition to verify that a portion of a line in a field contains what I am really looking for, I use tVariable "is not in" the field as much as I use fld "Field" contains... In fact, most of the time when I am using multiple conditions, I find that to really isolate the correct portion of a text, I need to use both true and false conditions in conjunction with each other.

So, how can I use an "is not in" with multiple conditions through a switch/case statement?

As an example:

case "tDinosaur"
case "tBrontosaurus"
case not "tRex"

I'm not actually building a dinosaur program, I just like that tRex is the original temporary variable, but I really am hoping that there's a way to accomplish this task that I have been unable to find in the documentation.

Cheers,
deeverd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Multiple Conditions & "is not in"

Post by dunbarx » Tue Jun 16, 2015 10:16 pm

Hi.

It is a matter of personal style which way you do this. That said, "switch" constructions are generally far more easily read, modified and written than "if-then". The inherent structure of switch usually subsumes the nesting required for multiple levels of "if then".

That said, you just need a little practice:

Code: Select all

switch
  case foo = 42
    UltimateAnswer
    break
  case 42 is not in the clipboardData
    answerLiesSomewhereElse
    break
  case ultimateQuestion()
    relaxAndGoWithIt
    break
end switch
Now the other way to do this is:

Code: Select all

put random(99) into foo
switch foo
  case 9
    doThis
    break
  case 2
    snakeEyes
    break
  case 125
    thisWillNeverFire
    break
end switch
You will find that switch usually requires no nested structures at all, though included switch or "if-then" are supported to any level as long as they are all valid LC constructs. But for most purposes, the gadget is just fine, single-level, as per the above. I found this one of the best enhancements coming from HC.

Craig Newman

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Multiple Conditions & "is not in"

Post by SparkOut » Tue Jun 16, 2015 10:30 pm

Each "case" contains a block of code that will be enacted if the case resolves to true.
Which means that it is perfectly feasible to resolve a negative case to true.
Eg
switch
case tRex is not tApatosaur
answer "These are not the same"
break
case tRex is not "herbivore"
answer "This is a meat eater"
break
end switch

Edit: slow, slow forum posting by phone.

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Multiple Conditions & "is not in"

Post by deeverd » Tue Jun 16, 2015 11:27 pm

Hello Craig and SparkOut,

I had no idea that "is not" could be used in Switch & Case. That will certainly make life a lot easier knowing this info.

Thank you both very much.
All the best,
Allen

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Multiple Conditions & "is not in"

Post by deeverd » Tue Jun 16, 2015 11:50 pm

Just realized after playing with the examples of the provided switch & case script that a main part of what I'm trying to figure out is still unclear to me...

To illustrate, here's some clunky code that definitely doesn't work but hopefully it illustrates what I'm after:

Code: Select all

on mouseUp
   switch
      case field "htmlField" contains "<b> "
      case "<i>" is not in field "htmlField"
      case field "htmlField" contains "<u> "
         beep 5	
         break
   end switch
end mouseUp
What I'm trying to do is to figure out, such as in the example above, how to make it beep 5 only if all three conditions are true. In other words, it should only beep 5 if the field contains "<b>" and contains "<u>" and does not contain "<i>"

So all three conditions have to be met at the same time to cause it to beep 5.

Is that possible?

Allen

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Multiple Conditions & "is not in"

Post by SparkOut » Tue Jun 16, 2015 11:59 pm

case field "htmlField" contains "<b>" and field "htmlField" contains "<u>" and "<i>" is not in field "htmlField"

If need be, enclose each test within ( )

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Multiple Conditions & "is not in"

Post by SparkOut » Wed Jun 17, 2015 12:06 am

For clarity, each case test is a check to see if the condition being tested resolves to a boolean value of true. Conditions can be strung together with and and or, as per any other boolean tests.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Multiple Conditions & "is not in"

Post by dunbarx » Wed Jun 17, 2015 12:20 am

Note that if you string cases together in a list, if any of them are satisfied, that case will fire. In other words:

Code: Select all

on mouseUp
   put "true" into y
   switch
      case x or y or z
         answer "Bingo"
   end switch
end mouseUp
is the same as:

Code: Select all

on mouseUp
   put "true" into y
   switch
      case x
      case y
      case z
         answer "Bingo"
   end switch
end mouseUp
Sparkout gave a different, er, case, when he said, essentially:
case x and y and z
Here all have to be true.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Multiple Conditions & "is not in"

Post by dunbarx » Wed Jun 17, 2015 12:24 am

Deeverd.
I had no idea that "is not" could be used in Switch & Case.
All valid LC constructs can be used in all valid LC constructs. Sometimes you have to pay attention to the case where LC will evaluate expressions before, say, passing parameters to other handlers. But this is for later...

Craig

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Multiple Conditions & "is not in"

Post by deeverd » Wed Jun 17, 2015 5:11 pm

Didn't get a chance to say that between the combination of both your advice, I am finally able to efficiently use multiple conditions including "contained" and "is not in" at the same time. That was a great help. It never dawned on me that using multiple conditions was a simple as placing them all on the same case line like this:

Code: Select all

on mouseUp
   switch
      case ((field "htmlField" contains "<b>") and (field "htmlField" contains "<u>") and ("<i>" is not in field "htmlField"))
         beep 5	
         break
   end switch
end mouseUp
I also tried using commas, semicolons, etc., all in experimentation, but as suggested, found out that good old parentheses and "and" was what worked.

You would think I was a complete newbie, but another surprising thing I hadn't realized was that boolean syntax works perfectly with LiveCode. Surprisingly, although I use "and" on a regular basis, I hadn't realized that I could also use "or"

It's nice to keep learning... even if it is a little embarrassing admitting what I didn't know sometimes.

Thanks so much for all your help.
Cheers,
Allen

Post Reply