replace if

Prev Next

Function Names

replace if

Description

This function compares the value in the 1st parameter with the comparison expression. If the value is true, then contents in the 2nd parameter replaces those in the 1st parameter. This function is particularly interesting in combination with vectorization where every element in sets provided will be compared, and, if appliable, the contents being replaced.

Vectorization: This function supports vectorization in the 1st and 2nd function parameter. Instead of providing a single value, you can provide a set or even a nested set which contain multiple values. The function will then process every value and its return value contains a corresponding set containing all results.

Call as: function

Restrictions

Indirect parameter passing is disabled
Vectorization is allowed in the 1st and 2nd function parameters

Parameter count

2

Parameters

No.TypeDescription
1
input
all types
set
Value to compare

This value will be compared with the comparison expression in the 3rd function parameter. Vectorization: In this case, the corresponding element in the set will apply.

2
input
all types
set
Value to replace

This value will be be returned if the comparison yields 'true'. Vectorization: In this case, the corresponding element in the set will apply.

3
code
comparison expression
:string
Comparison expression

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.

Return value

TypeDescription
all types
set
Compared and possibly replaced results

With vectorization applied, the comparison and replacements are performed for every element individually.

Examples

echo( replace if( 2, -1, >2 ) ); // False case, output 2
echo( replace if( 3, -1, >2 ) ); // True case, output -1
echo;
echo( replace if( {1,2,3,4,5}, -1, >2 ) ); // Mix of both

Output

2
-1

{1,2,-1,-1,-1}
Try it yourself: Open LIB_Function_replace_if.b4p in B4P_Examples.zip. Decompress before use.

See also

calc