A curious case

Jim Ault jimaultwins at yahoo.com
Thu Mar 3 15:26:01 EST 2011


On Mar 3, 2011, at 10:40 AM, Nonsanity wrote:
>
>  Are you sure the "is among" line works there?

I apologize for creating confusion with my example.
You are correct in that I mixed the TWO forms of the SWITCH structure  
in the same example.

     "among the words"
only works if you use the 'naked switch' form
    where you specify each test,
rather than test against a single variable

>
>  It sucks to build a whole switch
> structure, only to find out that you need to also test a second  
> variable for
> some cases, and have to convert the whole thing to if-elses after  
> the fact.
The following should let you see an alternative:
--------  script 1  ---------------------
    put word 1 of fld 1 into testValue
    put "second test" into conditionTwo

    SWITCH  -- no variable specified
       case testValue = "red"
       case testValue = "yellow"
       case testValue = "orange"
          put "red" into newFontColor
          break
       case testValue = "green"
       case testValue = "blue"
       case testValue = "purple"
          put "green" into newFontColor
          break
       case (testValue is among the words "pink coral azure maize") \
              and (conditionTwo is "second test")
          put "gray40" into newFontColor
          break
       case (conditionTwo is "second test")
          put "CornflowerBlue" into newFontColor
          break
       case (testValue is among the words "pink coral azure maize")
          put "Aquamarine" into newFontColor
          break
       default
          put "black" into newFontColor
    end switch

    set the textcolor of fld 1 to newFontColor
    ;put newFontColor

--------  script 2  ---------------------
put word 1 of fld 1 into testValue
SWITCH TESTVALUE  --using variable
  case "red"
  case "yellow"
  case "orange"
       put "red" into newFontColor
       break
  case "green"
  case "blue"
  case "purple"
       put "green" into newFontColor
       break
  default
       put "black" into newFontColor
end switch

set the textcolor of fld 1 to newFontColor
    ;put newFontColor

On Mar 3, 2011, at 10:40 AM, Nonsanity wrote:
>
>
> Are you sure the "is among" line works there?
>
> The case structure equates what comes after the "switch" keyword  
> with what
> comes after each "case", executing the code after first one to match  
> and all
> further code until it hits a "break". By those rules, you would only  
> get
> "darkBlue" if testValue contained "false". Since "false" is not in  
> the color
> list, the "is among" returns "false", which then matches the switch  
> value,
> so "darkBlue" is returned. But if testValue is "pink", you're going  
> to get
> "black" as a result.
>
> I can't test it right now, so I'm not sure, but that's how I read  
> the code.
> But if that code works as you say, then LC's switch flow control is  
> severely
> distorted from the norm!
>
> Personally, I avoid switch-case controls like the plague... UNLESS  
> my needs
> exactly match the control's two main strengths - one and only one  
> value to
> test, and fall-through concatenation of code blocks - AND the use is a
> simple, one-screenfull affair.
>
> Switches reek of the evil stench of gotos and taste of spaghetti. They
> aren't as readable as the rest of LC's code, which tends towards  
> spoken
> language on the whole.
>
> IF you can speak a switch statement in a sentence, THEN it might be  
> the
> better solution, ELSE you're probably better off just sticking with  
> if-else
> chains.
>
> ...Which are more flexible, anyway. It sucks to build a whole switch
> structure, only to find out that you need to also test a second  
> variable for
> some cases, and have to convert the whole thing to if-elses after  
> the fact.
>
> [This option may not be equal to the majority's view, but it's mine.]
>
> ~ Chris Innanen
> ~ Nonsanity

Jim Ault
Las Vegas






More information about the use-livecode mailing list