select/pick if existing ...

Prev Next

Function Names

select if existing, select if existing and valid, pick if existing, pick if existing and valid

Description

This function checks if the variable is existing. The function name ending .. existing and valid will also check the contents. True is returned as long the contents are not void. If the result is positive, then the variable contents will be returned. Otherwise, if a 2nd parameter is provided, the the value from the 2nd parameter is returned instead.

The function name beginning pick will calculate the 2nd parameter only if the condition for the 1st parameter is not met. This function is more useful when the 2nd parameter contains a more elaborate expression or possibly non-existing variables so skipping the code saves processing time.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1-2

Parameters

No.TypeDescription
1
code
variable
:string
Variable to check

This variable will be checked if it exists (and contains a valid value).

Opt. 2
input
all types Alternative value

Applicable to functions select if existing ... only

Alt. 2
code
expression
:string
Alternative expresion

Applicable to functions pick if existing ... only

Return value

TypeDescription
all types Selected value

Examples

  a[] = 123;
  b[] = null(); // Void value

  o0[] = select if existing           ( o[] ); // Variable does not exist, return void value
  o1[] = select if existing           ( o[], "Alternative value 1" ); // Variable does not exist
  a1[] = select if existing           ( a[], "Alternative value 2" ); // It exists
  a2[] = select if existing and valid ( a[], "Alternative value 2" ); // It exists
  b1[] = select if existing           ( b[], "Alternative value 3" ); // It exists
  b2[] = select if existing and valid ( b[], "Alternative value 4" ); // but contains no valid value

  echo("o0: ", o0[] );
  echo("o1: ", o1[] );
  echo("a1: ", a1[] );
  echo("a2: ", a2[] );
  echo("b1: ", b1[] );
  echo("b2: ", b2[] );

  // In the example below, o[] does not exist.  Since a[] is valid, the 2nd parameter will
  // not be evaluated by the 'pick if ...' function, and therefore a possible error message telling
  // that o[] is missing will not occur.  However, this does happen if you use 'select if ...'.

  a3[] = pick if existing ( a[], o[] ); // Note: 2nd variable does not exist
  echo("a3: ", a3[] );

Output

o0: # Invalid Value #
o1: Alternative value 1
a1: 123
a2: 123
b1: # Invalid Value #
b2: Alternative value 4
a3: 123
Try it yourself: Open LIB_Function_select_if_existing.b4p in B4P_Examples.zip. Decompress before use.

See also

existing
existing and valid
select if