in reply to Count duplicates in array.

It's rare that I think of a solution to a perl problem that DOESN'T involve regexes, hashes, or odd data structures:
#!/usr/bin/perl -w use strict; my @array=('bent','svend','gert','pete','svend','pete'); my $prev=""; my $count=0; foreach(sort @array) { if($prev ne $_) { if($count) { printf("%s:%d ",$prev,$count); } $prev=$_; $count=0; } $count++; } printf("%s:%d\n",$prev,$count) if $count;
Obviously, it could be golfed down somewhat, but I wanted to go for the clearer version.

The output isn't in the exact order that RuneK specified, but I'm not sure if that's important or not.
--
Mike

Replies are listed 'Best First'.
Re: Re: Count duplicates in array.
by RuneK (Sexton) on Nov 18, 2002 at 12:55 UTC
    Thanks for the many answers. It worked just fine.
    Br,
    Rune