filter ...

Prev Next

Function Names

filter, filter 1, filter n, filter start, filter start include, filter start n, filter start n include, filter stop, filter stop include

Description

This function extracts a middle part with those elements which fulfill the matching criteria specified in the 2nd parameter.

Unless specified otherwise, empty set is returned in case of no matches. The filter function does not check sets recursively.

Function Name Behavior
filter Returns all elements where matching criteria are met.
filter 1 Returns first match only.
filter n Limit the number of matches as specified in the 3rd parameter. 0 and negative numbers return empty sets.
filter start Returns all remaining elements after and excluding the first match.
filter start include Returns all remaining elemetns including and after the first match.
filter start n Return maximum specified number of elements after and excluding the first match.
filter start n include Return maximum specified number of elements including and after the first match.
filter stop Return all elements until before (excluding) the first match. Full set returned if no match identified.
filter stop include Return all elements until and including the first match.

This function may also be called as a procedure in case only the processed followers are needed.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

Min 2

Parameters

No.TypeDescription
1
input
set or string input set

If provided as a set: Will be used for counting elements
If provided as a quoted string: The string will be converted to a set containing one element.
If provided as a softquoted string: The comma-separated substrings will be converted to set elements. '' is equivalent to {}.

2
code
comparison expression
:string
Right-hand Comparison expression

This expression is used to compare every element of the set from left to right.
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.

Opt. 3
input
numeral Count

This parameter applies to function names filter n, filter start n and filter start n include. If provided as a set: Will be used for counting elements
If provided as a quoted string: The string will be converted to a set containing one element.
If provided as a softquoted string: The comma-separated substrings will be converted to elements. '' is equivalent to {}.

Opt. 3+ / Opt. 4+
io
set follower

Other sets in addition to the one provided as the first function parameter can be extracted in the same same way. Add any number of additional set variables. They will be manipulated at the exact same positions as the returned value.

Return value

TypeDescription
set Filtered set

Examples

               a[] = {A,1,2,3,4,E,5,2,6 };

               print( filter      ( a[], >= 3 ), ", " );    // {3,4,5,6}
               echo ( filter      ( a[], (2,3) ) );         // {2,3,2}

               print( filter 1    ( a[], >= 3 ), ", " );    // {3}
               echo ( filter n    ( a[], >= 3, 3  ) );      // {3,4,5}

               print( filter stop ( a[], >= 3 ), ", " );    // { A,1,2}
               echo ( filter stop include ( a[], >= 3 ) );  // { A,1,2,3}

               print( filter start    ( a[], 4 ), ", " );   // {E,5,2,6}
               echo ( filter start n  ( a[], (4), 3 ) );    // {E,5,2}

               // Why is (4) in parentheses?  This is a comparison and
               // should only be compared with 4, and not with both 4 and 3.
               // In the 2nd row, ( a[], (2,3) ) is same as ( a[], 2, 3 ) !
               // (4) is same as (=4). (2,3) is same as (=2,3).

               print( filter start include   ( a[], 4 ), ", " ); // {4,E,5,2,6}
               echo ( filter start n include ( a[], (4), 3 ) );  // {4,E,5}

               // The follower feature:

               b[] = {a,b,c,d,e,f,g,h,i};
               c[] = {A,B,C,D,E,F,G,H,I};

               filter      ( a[], (=2), b[], c[] ); // Attn: (=2) in parentheses
               echo( b[], " / ", c[] ); // Outputs {c,h}, {C,H}

               b[] = {a,b,c,d,e,f,g,h,i};
               c[] = {A,B,C,D,E,F,G,H,I};

               filter start n ( a[], (=2), 3, b[], c[] ); // Attn: (=2) in parentheses
               echo( b[], " / ", c[] ); // Outputs {d,e,f}, {D,E,F}

Output

{3,4,5,6}, {2,3,2}
{3}, {3,4,5}
{'A',1,2}, {'A',1,2,3}
{'E',5,2,6}, {'E',5,2}
{4,'E',5,2,6}, {4,'E',5}
{'c','h'} / {'C','H'}
{'d','e','f'} / {'D','E','F'}
Try it yourself: Open LIB_Function_filter.b4p in B4P_Examples.zip. Decompress before use.