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