Or you can use map although many do not like using map in a void context others find it more intuitive to operate on the entire array than looping through it's elements, Horses for courses. Map is slightly slower than foreach in this sort of case. If speed matters see this Re^4: how to avoid nested looping? for an excellent benchmark of map and for(each)? ).
my @array = qw/aa bb cc aa cc cc dd aa aa/; my %sums; map {$sum{$_}++} @array; print "$_ = $sums{$_}\n" foreach sort keys %sums;
Cheers,
R.
In reply to Re: Counting unique elements in an array
by Random_Walk
in thread Counting unique elements in an array
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |