Notifications
Clear all

[Closed] Question about case expressions

So, as my thread title suggests, I have a question about case expressions. If I am testing for a singular label value, I can set up a case expression as either:

“first method”:

case of
  (
 	(testVal == 1): format "testVal is 1
"
  )

or

“second method”:

case testVal of
 (
 	1: format "testVal is 1
"
 )

However, say I am testing for multiple label values, as shown here:

case of
 (
 	(testVal == 1 or testVal == 2): format "testVal is either 1 or 2
"
 )

Is there any corresponding “second method” syntax for this?? Obviously the following doesn’t work:

case testVal of
 (
 	(1 or 2): format "testVal is either 1 or 2
"
 )

Am I restricted to using the first method in this situation?

3 Replies

Hi,

I think no. If you want two differnt results to perform the same action then call a function from your case expression


fn result test=format "test	: %
" test
test=1

case test of
(
	1 : result test
	2 : rseult test
	
)

Josh.

EDIT: sorry was typing this while j-man posted. I don’t mean to be redundant.

Second method is the proper format.

If you want multiple conditions you need to list them each:


 case testVal of
  (
 	 1: format "testVal is either 1 or 2
"
 	 2: format "testVal is either 1 or 2
"
 	 default: format "testVal is neither 1 or 2
"
  )
 

if you are worried about duplicating code in each line make a function for common cases:


  fn doSomething val=
 (
 	 format ("testVal is " + (val as string))
 )
 case testVal of
   (
  	 1: doSomething testVal
  	 2: doSomething testVal
  	 default: format "testVal is neither 1 or 2
"
   )
  

You may want to consider just using if/else when you only have a couple case values. (i’m sure though that this is just a small sample of a bigger case)

Does this help?

Okay, thanks for the confirmation. That’s pretty much how I’ve been doing it, but I looked at my script today and was like “is this really the best way???”