in reply to Count duplicates in array.
Obviously, it could be golfed down somewhat, but I wanted to go for the clearer version.#!/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;
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 |