Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationlist_count_if

list_count_if (list l, function comparator_fn) ⇒ number

Sequentially visits each item in the list and compares it with a comparator function. If the function returns true, then the counter is increased.

First available: Version 1.8.3

Notes

This built-in function is fast and powerful. Using the comparator function you can specify your own criteria for what you are looking for but you can also process the data as it comes through.

YASS
$l = [10, 11, 12, 31, 32, 33]
$large = []
$small = []

$total = list_count_if($l, function ($x) {
  if ($x > 20) {
    list_add_element($large, $x)
    return true
  } else {
    list_add_element($small, $x)
    return false
  }
})

print($total)
print($large)
print($small)
Do not forget to return false in the inner function since a function with no return value returns null which is not the same as false and can evaluate to true.
Comments
Feedback 👍
Comments are sent via email to me.