To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Official ZPE/YASS documentationcounter

counter (list items[, function f]) ⇒ associative_array

Generates an associative array of the count of all instances of a string. If a function is supplied to this, each item will be passed to the function first.

First available: Version

Using counter

Using a function is useful when you are working with an array of arrays, since the function compares based on the value as a whole. Given some lambda function, only a part of the inner arrays could be returned and processed. The counter function supports the optional f parameter.

The following example is a standard use of counter.

function main($args)

  //This variable used to be called $list, but since ZPE 1.5, list is a reserved keyword and cannot be used
  $l = [["Ashley", 32], ["Jonathan", 64], ["Adam", 12], ["Jamie", 32], ["Frank", 64]]

  //By passing this to the function, we can return the second index (index 1) from the inner list and count that
  $counted = counter($l, function($x) return list_get_at_index ($x, 1) end function)

  //Should return [12 => 1, 64 => 2, 32 => 2]

end function

There are no comments on this page.

New comment
Feedback 👍
Comments are sent via email to me.