compare select / pick

Prev Next

Function Names

compare select, compare pick

Description

These two functions carry out a sequence of comparisons with comparison expressions provided and then return the corresponding value in the following parameter.

Call as: procedure or function

Restrictions

Indirect parameter passing is enabled for 'select by value'.

Parameter count

4, 6, 8, etc.

Parameters

No.TypeDescription
1
input
valid types Value to compare

The value provided in the 1st function parameter will be compared with the comparison expresssions in the following odd-numbered parameters.

2
input
boolean Default Value

Applicable to compare select: This is the default value to be returned if all comparisons specified in the following function parameters turned out false.

Alt. 2
code
expression
:string
Default Expression

Applicable to compare pick: This is the default expression to be calculated if all comparisons in the following function parameters turned out false.

3, 5, ...
code
comparison expression
:string
Values to compare with

The value provided in the 1st function parameter will be compared with the comparison expressions provided in these odd-numbered function parameters. Once a positive match (comparison returns true) has been found, then comparison will stop and the value from the next function aparameter will be returned. This parameter is a piece of code typically found on the right-hand side of a comparison with '=' or '<>'. Single values, ranges (e.g. 3..5), multiple values separated by commas are supported. For text comparison, wildcards are supported if the string is of type softquoted string.

Attention: To avoid hijacking further function parameters as additional values to be compared, put the expression into parentheses if no operators, = or <> have been used. This hijacking issues does not apply if the following operators are used: <, >, ==, != .

4, 6, ...
input
boolean Value to select

Applicable to compare select: This value will be returned if a match with the previous parameter applies.

Alt. 4, 6, ...
code
expression
:string
Expression to select

Applicable to compare pick: This expression will be calculated and the value returned if a match with the previous parameter applies. Otherwise, the expression will not be calculated.

Return value

TypeDescription
all types Selected value

Examples

  echo( "Demonstrate 'compare pick': 'Greater than 5' is only printed out when the value is needed");
  for (i[] = 1, i[] <= 10, i[]++)
  {
      a[] = compare select( i[], Others, (2,5,7), "Is 2 or 3", >5, print("Greater than 5  ") );
      echo( i[], ": ", a[] );
  }

  echo( new line, "Demonstrate 'compare pick': 'Greater than 5' is only printed out when the value is needed");
  for (i[] = 1, i[] <= 10, i[]++)
  {
      a[] = compare pick ( i[], Others, (2,5,7), "Is 2 or 3", >5, print("Greater than 5  ") );
      echo( i[], ": ", a[] );
  }

Output

Demonstrate 'compare pick': 'Greater than 5' is only printed out when the value is needed
Greater than 5  1: Others
Greater than 5  2: Is 2 or 3
Greater than 5  3: Others
Greater than 5  4: Others
Greater than 5  5: Is 2 or 3
Greater than 5  6: Greater than 5  
Greater than 5  7: Is 2 or 3
Greater than 5  8: Greater than 5  
Greater than 5  9: Greater than 5  
Greater than 5  10: Greater than 5  

Demonstrate 'compare pick': 'Greater than 5' is only printed out when the value is needed
1: Others
2: Is 2 or 3
3: Others
4: Others
5: Is 2 or 3
Greater than 5  6: Greater than 5  
7: Is 2 or 3
Greater than 5  8: Greater than 5  
Greater than 5  9: Greater than 5  
Greater than 5  10: Greater than 5  
Try it yourself: Open LIB_Function_compare_select.b4p in B4P_Examples.zip. Decompress before use.

See also

select ifs
pick ifs
select by value
pick by value