📘 Counting the number of occurrences in array using Perl 6

📘 Counting the number of occurrences in array using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


Count how many times a particular element appears in the array.

Let’s have an array with a list of fruits; some of them are repeated.

my @data = <
    apple       pear      grape     lemon
    peach       apple      banana   grape 
    pineapple  avocado
>;

Now, the task is to count how many times the element 'grape' appears there. First, extract all the elements to a temporary array by using the grep method, and then call the elems method to get the number of elements in the filtered data.

my $n = @data.grep('grape').elems;
say $n; # 2

Notice also a very handy way of defining arrays of strings. Instead of quoting all the elements, use the <...> syntax to create a list out of space-separated values. 

If you need to determine the number of occurrences for more than one element, then it is better to use the more efficient technique to avoid grepping the array for every item.

my %count;
%count{$_}++ for @data;

say %count<pineapple>; # 1
say %count<grape>;     # 2

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code