choose() Function

Evaluates the choice argument at the given index and returns the result.

Syntax

choose( index, choice1, choice2, … )

index: (Integer) The one-based index that selects which choice argument to evaluate and return.

choice1: (Any Type) The first of n possible choices, where n must be >= 1. Only the selected value is evaluated.

Returns

Any Type

Notes

The index parameter must be a scalar number between 1 and the number of choices. It cannot be null.

If the selected choice evaluates to null, null will be returned.

Unlike most functions, choose does not evaluate all its arguments. First the index is evaluated, then the selected value arguments. The unchosen arguments are not evaluated. Use choose to avoid calling potentially costly expressions whose results would be discarded.

Examples

You can experiment with this function in the test box below.

Test Input

Use expressions as the choice arguments or as the index argument.

choose(2,2+2,4/2,3^2) returns 2

choose(weekday(today()),"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") returns the name of the day of the week

It can be useful to use the wherecontains function in conjunction with choose. Since wherecontains returns a list, use the first result for the index parameter.

1
2
3
4
5
6
choose(
  wherecontains(ri!rating, {"Poor","Satisfactory","Excellent"})[1],
  mod(rand() * 10, 5),
  mod(rand() * 10, 5) + 4,
  10 - mod(rand() * 10, 2)
)

See also: wherecontains()

FEEDBACK