in reply to Counting unique elements in an array
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Counting unique elements in an array
by Roy Johnson (Monsignor) on Feb 08, 2005 at 17:04 UTC | |
by Random_Walk (Prior) on Feb 08, 2005 at 18:01 UTC | |
by Roy Johnson (Monsignor) on Feb 08, 2005 at 18:54 UTC | |
by Random_Walk (Prior) on Feb 08, 2005 at 20:28 UTC | |
|
Re^2: Counting unique elements in an array
by revdiablo (Prior) on Feb 08, 2005 at 18:01 UTC |