select, pick

Prev Next

Function Names

select, pick

Description

This function selects a following parameter depending on the value of the first parameter. Positive numbers count from left to right, where 0 refers to the 2nd function parameter. Negative numbers count from right to left, where -1 refers to the last parameter.

select will calculate the expressions in all parameters.
pick will calculate the expression of the applicle parameter and skip calculating the other ones. This approach saves processing time, especially if the expressions contain heavy calculations or large contents (e.g. parameter sets retrieved from tables).

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

Min. 2

Parameters

No.TypeDescription
1
input
numeral Number als selector

0, 1, 2, etc. select 2nd, 3rd, 4th, etc. function parameter. -1, -2, etc. selects the last function parameter.

2, etc.
input
all types Selected value

Applicable to select: The values of all function parameters will be calculated. The selected value will be returned.

Alt. 2, etc.
code
expression
:string
Selected expression

Applicable to pick: Only the expression of the selected parameter will be calculated. The other expressions will be skipped.

Return value

TypeDescription
all types Selected value

Exceptions

Value is out of bounds

Examples

  a[] = 1; b[] = 2; c[] = 3; d[] = 4;

  echo("Function 'select':");
  for (i[] = 0, i[] <= 3, i[]++ )
  {
      print( select( i[], a[]++, b[]++, c[]++, d[]++ ), "  " );
  }
  // Note: All 4 variables have been incremented 4 times.
  echo(new line, " a[]..d[] = ", a[], " ", b[], " ", c[], " ", d[], " " );
  echo;

  a[] = 1; b[] = 2; c[] = 3; d[] = 4;

  echo("Function 'pick':");
  for (i[] = 0, i[] <= 3, i[]++ )
  {
      print( pick( i[], a[]++, b[]++, c[]++, d[]++, e[]++ ), "  " );
      // Note: e[] added, but will never be executed, so no error message occurs (variable not existing)
  }
  // Note: All 4 variables have been once only
  echo(new line, " a[]..d[] = ", a[], " ", b[], " ", c[], " ", d[], " " );

Output

Function 'select':
1  3  5  7  
a[]..d[] = 5 6 7 8

Function 'pick':
1  2  3  4  
a[]..d[] = 2 3 4 5
Try it yourself: Open LIB_Function_select.b4p in B4P_Examples.zip. Decompress before use.

See also

select if
pick if