Compares expressions and returns the value of the first matching case.
Synopsis
$CASE(target,case:value,case:value,...,:default)
Parameters
target A literal or expression the value of which is to be matched against cases.
case A literal or expression the value of which is to be matched with the results of the evaluation of target.
value The value to be returned upon a successful match of the corresponding case.
default Optional — The value to be returned if no case matches target.
Description
The $CASE function compares target to a list of cases (literals or expressions), and returns the value of the first matching case. An unlimited number of case:value pairs can be specified. Cases are matched in the order specified (left-to-right); matching stops when the first exact match is encountered.
If there is no matching case, default is returned. If there is no matching case and no default is specified, an error is returned.
Parameters
target
$CASE evaluates this expression once, then matches the result to each case in left-to-right order.
case
A case can be a literal or an expression; matching of literals is substantially more efficient than matching expressions, because literals can be evaluated at compile time. Each case must be paired with a value. An unlimited number of case and value pairs may be specified.
value
A value can be a literal or an expression. Using $CASE as an argument of a GOTO command or a DO command restricts value as follows:
default
The default is specified like a case:value pair, except that there is no case specified between the comma (used to separate pairs) and the colon (used to pair items). The default is always the final parameter specified in a $CASE function. The default value follows the same GOTO and DO restrictions as the value parameter.
Example
The following example takes a numeric input and writes out the appropriate explanatory string:
  READ "Input a number 1-3: ",x
  SET multi=$CASE(x,1:"single",2:"double",3:"triple",:"input error")
  WRITE multi
See Also