Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How do I count the number of occurrences of a string in an array?

by pme (Prior)
on Aug 13, 2015 at 17:09 UTC ( #1138457=note: print w/replies, xml ) Need Help??


in reply to How do I count the number of occurrences of each string in an array?

Hi aschwa,

Welcome to the monastery!

You can simply convert the array into a hash.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = qw(dog cat sheep dog dog cat); my %hash; $hash{$_}++ for @array; # dump the hash to stdout print Dumper( \%hash ) . "\n"; # print each key-value pairs for (sort keys %hash) { print "$_ -> $hash{$_}\n"; }
  • Comment on Re: How do I count the number of occurrences of a string in an array?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1138457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-01-20 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?