Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.

For information on migrating to InterSystems IRIS, see Why Migrate to InterSystems IRIS?

$Case Function

The $Case function evaluates an expression (its first argument), and returns the result that corresponds to the value of the expression (the rest of the arguments). The last argument can be a default result, returned if no other argument value matches the expression.

$Case can return literals, as shown in the first example, or it can return names of procedures or routines, when used as the argument of a Do, as in the second example.

SAMPLES>set surv = 3

SAMPLES>write $case( surv, 1:"Rich", 2:"Kelly", 3:"Rudy", 4:"Sue", :"")
SAMPLES>set surv = 1

SAMPLES>do $case( surv, 1:celebrate^survivor() , :complain^survivor() )
Yippee! I won!

The survivor.mac code:

survivor ; celebrate or complain
celebrate() PUBLIC
    { Write !, "Yippee! I won!" }

complain() PUBLIC
    { write !, "Oh well, I lost." }