choose() Function

Function

choose( key, choice1, choiceN )

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

See also: wherecontains()

Parameters

Keyword Type Description

key

Number (Integer)

The one-based index that selects which choice argument to evaluate and return.

choice1

Any Type

The first of n possible expressions, where n must be >= 1. Only the selected value is evaluated.

choiceN

Any Type

An unlimited number of expressions.

Returns

Any Type

Usage considerations

Working with nulls

  • If the selected choice evaluates to null, null will be returned.
  • The index parameter must be a scalar number between 1 and the number of choices. It cannot be null.

Evaluation order

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)
	)
Open in Github Built: Fri, Oct 22, 2021 (11:11:24 AM)

On This Page

FEEDBACK