in reply to Counting unique elements in an array

Anonymous Monk,
This is one of the functions I was thinking about adding to RFC: Tool::Box. What you haven't said is if you want the output in any order, Asciibetical order, the order of first appearance, or some other order. Since no one has given you the "in the order of first appearance", here is my offer:
my @array = qw(bb aa dd cc aa aa cc aa aa cc); print "$_\n" for uniq_ordered_count( \@array ); sub uniq_ordered_count { my $list = shift; my %count; $count{ $_ }++ for @$list; return map { exists $count{$_} ? "$_ - " . delete $count{$_} : () +} @$list; }

Cheers - L~R

Update: Per bart's recommendation. I changed the wording of original order to first appearance.

Replies are listed 'Best First'.
Re^2: Counting unique elements in an array
by Anonymous Monk on Feb 08, 2005 at 14:15 UTC
    Or without a sub:
    my @array = qw(bb aa dd cc aa aa cc aa aa cc); print "$_ $_{$_}\n" for grep !$_{$_}++, @array;
    Localize %_ if needed.