count elements ...

Prev Next

Function Names

count elements, count elements ignore case, count elements ignore blanks, count elements ignore both

Description

Counts the number of elements which match with the supplied value. The ordering of the elements in the parameter sets do not matter.

Attention: Comparing Boolean true and string 'true' (lower case letters) will also count because comparison is based on the single '=' comparison operator which returns true if true = "true" and vice versa.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

2

Parameters

No.TypeDescription
1
input
parameter set or string input parameter set

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

2
input
valid types matching value

The comparison looks into both value and type. Providing a numeral 1 is not the same as a string '1'. The parameter set will be compared with this value in order to count matches. The options 'ignore case', 'ignore blanks' and both combined will only apply if the values compared are strings.
If the value is an unquoted string or softquoted string, then wildcard symbols are supported.

Return value

TypeDescription
numeral Number of elements identified

Number of elements (nested elements are not counted)

Examples

      print ( count elements( { Amy, Amsel, Bea, Beat, Charles, Charly }, 'Am*' ),", " ); // 2
      print( count elements( { Amy, Amsel, Bea, Beat, Charles, Charly }, "Am*" ), ", " ); // 0
      echo ( count elements ignore case( { Amy, Amsel, Bea, Beat, Charles, Charly }, amsel ) ); // 1
      print ( count elements( { 1, true, false, true, 2 }, true ), ", " ); // 2
      print ( count elements( { 1, true, false, true, 2 }, "true" ), ", " ); // 2 (true = 'true') case
      echo( count elements( { 1, '1', 2, '2' }, 'true' ) ); // 0

      // Subsets
      print( count elements( { a,b,c,{d,e} }, {d,e} ), ", " ); // 1
      echo( count elements( { a,b,c,{d,e} }, {e,d} ) ); // 1

Output

2, 0, 1
2, 2, 0
1, 1
Try it yourself: Open LIB_Function_count_elements.b4p in B4P_Examples.zip. Decompress before use.

See also

length [parameter set function]
length of parameter sets