Chapter 6 Caché Function Collection $CASE Function

Article Directory

Chapter 6 Caché Function Collection $CASE Function

Compare expressions and return the first value that matches the case.

Outline

$CASE(target,case:value,case:value,...,:default)

parameter

  • target The text or expression whose value should match the case.
  • The value of the case text or expression should match the result of the target calculation.
  • value The value to be returned after successfully matching the corresponding case.
  • default Optional-the value returned if there is no case matching target.

description

$CASEThe function compares the target with a list of cases (literal or expression) and returns the value of the first matching case. An unlimited number of case:valuepairs can be specified . $CaseMatches are performed in the specified order (from left to right); when the first exact match is encountered, the matching stops.

If there is no matching case, the default value is returned. If there is no matching case and no default value is specified, Caché will issue an error.

Caché allows no case:valuepair designation $CASE. Regardless of the target value, it always returns to the default value.

parameter

target

$CASEEvaluate the expression once, and then match the result to each case in order from left to right.

case

The case can be a literal or an expression; the matching of the literal is much more effective than the matched expression, because the literal can be evaluated at compile time. Each CASEmust be paired with a value. You can specify an unlimited number of cases and value pairs.

value

The value can be a text or an expression. Using $CASEas a GOTOcommand or DOa parameter of a command limits the value as follows:

  • When using $CASEstatements with GOTOcommands, each value must be a valid line label. It cannot be an expression.
  • When using $CASEstatements with DOcommands, each value must be a valid DOparameter. These DOparameters can include parameters.

default

By default, it is specified as a case:valuepair, but there is no case between the comma (used to separate pairs) and the colon (used to pair items). The default is optional. If specified, it will always be $CASEthe final parameter in the function. The default value follows the same GOTOsum DOrestrictions as the value parameter .

If there is no matching case and no default value is specified, Caché will issue an <ILLEGAL VALUE>error.

Example

The following example takes the day of the week and returns the corresponding day name. Please note that the default value " entry error" is provided:

/// d ##class(PHA.TEST.Function).Case()
ClassMethod Case()
{
    
    
	SET daynum=$ZDATE($HOROLOG,10)
	WRITE $CASE(daynum,
				1:"Monday",2:"Tuesday",3:"Wednesday",
				4:"Thursday",5:"Friday",
				6:"Saturday",7:"Sunday",:"entry error")
}
DHC-APP>d ##class(PHA.TEST.Function).Case()
Wednesday

The following example takes as input the number of bases obtained by a baseball batter and writes the appropriate baseball term:

/// d ##class(PHA.TEST.Function).Case1()
ClassMethod Case1()
{
    
    
	SET hit=$RANDOM(5)
	SET atbat=$CASE(hit,1:"single",2:"double",3:"triple",4:"home run",:"strike out")
	WRITE hit," = ",atbat
}

DHC-APP>d ##class(PHA.TEST.Function).Case1()
0 = strike out
DHC-APP>d ##class(PHA.TEST.Function).Case1()
2 = double
DHC-APP>d ##class(PHA.TEST.Function).Case1()
3 = triple
DHC-APP>d ##class(PHA.TEST.Function).Case1()
1 = single
DHC-APP>d ##class(PHA.TEST.Function).Case1()
4 = home run

The following examples are used $CASEas docommand parameters. It calls EXPthe routine applicable to the index value:

/// d ##class(PHA.TEST.Function).Case2()
ClassMethod Case2()
{
    
    
Start  ; Raise an integer to a randomly-selected power.
	SET exp=$RANDOM(6)
	SET num=4
	DO $CASE(exp,0:NoMul(),2:Square(num),3:Cube(num),:Exponent(num,exp))
	WRITE !,num," ",result,!
  	RETURN
Square(n)
	SET result=n*n
	SET result="Squared = "_result
	RETURN
Cube(n)
	SET result=n*n*n
	SET result="Cubed = "_result
	RETURN
Exponent(n,x)
	SET result=n
	FOR i=1:1:x-1 {
    
     SET result=result*n }
	SET result="exponent "_x_" = "_result
	RETURN
NoMul()
	SET result="multiply by zero"
	RETURN
}
DHC-APP>d ##class(PHA.TEST.Function).Case2()
 
4 exponent 5 = 1024
 
DHC-APP>d ##class(PHA.TEST.Function).Case2()
 
4 exponent 1 = 4
 
DHC-APP>d ##class(PHA.TEST.Function).Case2()
 
4 Squared = 16
 
DHC-APP>d ##class(PHA.TEST.Function).Case2()
 
4 multiply by zero

The following example tests whether the character input is letters or other characters:

/// d ##class(PHA.TEST.Function).Case3()
ClassMethod Case3()
{
    
    
	READ "Input a letter: ",x,!
	SET chartype=$CASE(x?1A,1:"letter",:"other")
	WRITE chartype
}
DHC-APP>d ##class(PHA.TEST.Function).Case3()
Input a letter: a
letter
DHC-APP>d ##class(PHA.TEST.Function).Case3()
Input a letter: 1
other

The following example uses to $CASEdetermine which subscript variable to return:

/// d ##class(PHA.TEST.Function).Case4()
ClassMethod Case4()
{
    
    
	SET dabbrv="W"
	SET wday(1)="Sunday",wday(2)="Monday",wday(3)="Tuesday",
	   wday(4)="Wednesday",wday(5)="Thursday",wday(6)="Friday",wday(7)="Saturday"
	WRITE wday($CASE(dabbrv,"Su":1,"M":2,"Tu":3,"W":4,"Th":5,"F":6,"Sa":7))
}

DHC-APP>d ##class(PHA.TEST.Function).Case4()
Wednesday

The following example does not specify a case:valuepair. It returns the default string " not defined":

/// d ##class(PHA.TEST.Function).Case5()
ClassMethod Case5()
{
    
    
	SET dummy=3
	WRITE $CASE(dummy,:"not defined")
}
DHC-APP>d ##class(PHA.TEST.Function).Case5()
not defined

Guess you like

Origin blog.csdn.net/yaoxin521123/article/details/108252210