Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

"else" with 'switch

 [1/7] from: norsepower:uswest at: 17-Feb-2001 13:44

Getting closer. But how come...
>> testfunc "B"
C Why doesn't 'switch work within 'either? testfunc: func [option][ either option = any [("A")("B")][ switch option [ "A" [print "A"] "B" [print "B"] ] ][ print "C" ] ] -Ryan

 [2/7] from: norsepower:uswest at: 17-Feb-2001 12:13

I'm trying to develop else-like logic using 'switch... testfunc: func [option][ switch option [ "A" [print "A"] "B" [print "B"] (not (all ["A" "B"])) [print "C"] ] ] The above function doesn't work. Is there a way to get this to work? -Ryan

 [3/7] from: al:bri:xtra at: 18-Feb-2001 9:27

Ryan wrote:
> I'm trying to develop else-like logic using 'switch...
What's wrong with /Default for 'Switch?
>> help switch
USAGE: SWITCH value cases /default case DESCRIPTION: Selects a choice and evaluates what follows it. SWITCH is a function value. ARGUMENTS: value -- Value to search for. (Type: any) cases -- Block of cases to search. (Type: block) REFINEMENTS: /default case -- Default case if no others are found. (Type: any) Like this: switch/default option [ "a" [print "a"] "b" [print "b"] ][ print "something else" ] Andrew Martin ICQ: 26227169

 [4/7] from: al:bri:xtra at: 18-Feb-2001 9:31

Ryan wrote:
> Why doesn't 'switch work within 'either? > > testfunc: func [option][ > either option = any [("A")("B")][ > switch option [ > "A" [print "A"] > "B" [print "B"]
'any tests for logic condition being false or none. To get your intent, try: any [ option = "A" option = "B" ] to return true if option is "A" or "B". I hope that helps! Andrew Martin ICQ: 26227169

 [5/7] from: ptretter:norcom2000 at: 17-Feb-2001 15:26

The problem is that ANY will return on the first false value it finds during evaluation. The core.pdf is a bit confusing here but my experience is that if you ensure that the values you are evaluating are either true or none you will be safe. In your case since you use an operator "=" for the evaluation you will get either a true or false result. This means that if 'option is B then you will get "C" because it reads the evaluation as False as soon as it compares to "A". Therefore use the find function instead of an operator for this evaluation which will result in either none or true during evaluation. For example:
>> value: "A"
== "A"
>> value = "B"
== false
>> find value "B"
== none So lets recap - ANY will stop evaluation as soon as it encounters a FALSE evaluation. ANY will continue evaluation as long as the result is not FALSE this includes TRUE and NONE evaluations. ANY will return or select the value that evaluates to TRUE. Lets rewrite your function: testfunc: func [option][ either any [ find option "A" find option "B" ][ switch option "A" [print "A"] "B" [print "B"] ] ][ print "C" ] ]
>> testfunc "A"
>> testfunc "B"
>> Testfunc "C"
>> Testfunc "D"
C I hope this helps. Paul Tretter

 [6/7] from: al::bri::xtra::co::nz at: 18-Feb-2001 11:39

Another way is: if found? find ["A" "B"] option [ print "option is A or B"] Andrew Martin ICQ: 26227169

 [7/7] from: norsepower:uswest at: 17-Feb-2001 14:39

Exactly what I'm looking for. Thank you.