Compares expressions and returns
the value of the first matching case.
Synopsis
$CASE(target,case:value,case:value,...,:default)
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.
$CASE evaluates this expression once, then matches
the result to each
case in left-to-right order.
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.
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:
-
When using a
$CASE statement with a
GOTO command,
each
value must be a valid tag. It cannot be an expression.
-
When using a
$CASE statement with a
DO command,
each
value must be a valid
DO argument.
These
DO arguments can include parameters.
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.
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