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 1.4.1.0

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.

YASS
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}
  print($counted)

end function
        
Comments

There are no comments on this page.

New comment
Reply
Provide feedback on this page
Comments are sent via email to me.