Skip to main content

$SELECT (ObjectScript)

Returns the value associated with the first true expression.

Synopsis

$SELECT(expression:value,...)
$S(expression:value,...)

Arguments

Argument Description
expression The select test for the associated value argument.
value The value to be returned if the associated expression evaluates to true.

Description

The $SELECT function returns the value associated with the first expression that evaluates to true (1). Each $SELECT argument is a pair of expressions separated by a colon. The left half is a boolean expression that evaluates to 1 (true) or 0 (false). The right half is a value to be returned; value can be any expression. Any number of comma-separated expression:value pairs can be specified.

In the following example, the truth values of the first three expressions are tested; if none of them evaluate to true, the final expression (which always evaluates to true) returns its value:

  WRITE $SELECT(x=1:"1st is True",x=2:"2nd is True",x=3:"3rd is True",1:"The Default")

$SELECT evaluates the expressions from left to right. When $SELECT discovers a truth-valued expression with the value of true (1), it returns the matching expression to the right of the colon. $SELECT stops evaluation after it discovers the left-most true truth-valued expression. It never evaluates later pairs on the argument list.

You can construct complex logic by nesting $SELECT functions. Like all evaluated truth conditions, a NOT logical operator (') can be applied to a nested $SELECT.

Arguments

expression

The select test for the associated value argument. It can be any valid InterSystems IRIS relational or logical expression. If no expression evaluates to true, the system generates a <SELECT> error. To prevent an error from disrupting an executing routine, the final expression can be the value 1, which always evaluates to true.

When expression is a string or numeric, any non-zero numeric value evaluates to true. A zero numeric value or a non-numeric string evaluates to false.

value

The value to be returned if the associated expression evaluates to true. It can be a numeric value, a string literal, a variable name, or any valid ObjectScript expression. If you specify an expression for value, it is evaluated only after the associated expression evaluates to true. If value contains a subscripted global reference, it changes the naked indicator when it is evaluated. For this reason, be careful when using naked global references either within or immediately after a $SELECT function. For more details on the naked indicator, see Naked Global Reference.

Examples

To ensure that a <SELECT> error never results, you should always include the value 1 as the last expression with an appropriate default value. This is shown in the following example:

Start
   READ !,"Which level?: ",a 
   QUIT:a=""
   SET x=$SELECT(a=1:"Level1",a=2:"Level2",a=3:"Level3",1:"Start")
   DO @x
Level1()
   WRITE !,"This is Level 1"
Level2()
   WRITE !,"This is Level 2"
Level3()
   WRITE !,"This is Level 3"

If the user enters a value other then 1, 2, 3, or the null string, control is passed back to the top of the routine.

You can use $SELECT to replace multiple IF clauses. The following example uses IF, ELSEIF, and ELSE clauses to determine whether a number is odd or even:

OddEven()
   READ !,"Enter an Integer: ",x
   QUIT:x=""
   WRITE !,"The input value is "
   IF 0=$ISVALIDNUM(x) { WRITE "not a number" }
   ELSEIF x=0 { WRITE "zero" }
   ELSEIF ""=$NUMBER(x,"I") { WRITE "not an integer" }
   ELSEIF x#2=1 { WRITE "odd" }
   ELSE  { WRITE "even" }
   DO OddEven

The following example also accepts a number and determines if the number is odd or even. It uses $SELECT to replace the IF command in the previous example:

OddEven()
   READ !,"Enter an Integer: ",x
   QUIT:x=""
   WRITE !,"The input value is "
   WRITE $SELECT(0=$ISVALIDNUM(x):"not a number",x=0:"zero",
                 ""=$NUMBER(x,"I"):"not an integer",x#2=1:"odd",1:"even")
   DO OddEven

$SELECT and $CASE

Both $SELECT and $CASE perform a left-to-right matching operation on a series of expressions and return the value associated with the first match. $SELECT tests a series of boolean expressions and returns the value associated with the first expression that evaluates true. $CASE matches a target value to a series of expressions and returns the value associated with the first match.

See Also

FeedbackOpens in a new tab