Operator Type:
Operator Scope of Action:
Operator Purpose:
Operator First Added:
Operator Last Altered:
Operator Has Conditional Arguments:
Operator Uses Loop Variable Argument:
Function [other Function type actions]
Group [operators of similar scope]
Data manipulation [other Data manipulation operators]
Baseline
As at baseline
List/Set.count_if(loopVar, condition)
The dot-operator .count_if() counts the members of a list that satisfy a condition. Each item in the list is bound in turn to loopVar, and if condition is met, then that item is added to the returned count.
loopVar is essentially the same as the loop variable used by the List.each() operator. In the examples below, for clarity the loopVar value "anItem" is used, but as with any loop variable a shorter less expressive values such a "x" can be used (e.g. by more expert users).
The condition argument is a conditional expression for which each tested item must return true
or false
.
For example if $MyList is "1;2;3;4;5", then:
$MyListA =- $MyList.count_if(anItem, anItem>3)
returns 2
, being the number of items in $MyList whose value is greater than 3. If $MyList is "1;1;2;2;3;3;4;4;5;5", then:
$MyListA =- $MyList.count_if(anItem, anItem>3)
returns 4
, being the number of items in $MyList whose value is greater than 3 and values '4' and '5' each occur twice.
List/Set.count_if() vs. .count_if()
Although the two appear similar. This operator works directly on the source list values, whereas count_if() creates a list of $Path values and returns on an attribute value from each of those paths (where the item at the $Path meets the condition).