RuneK has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I have an array with duplicates in it and I would like to count how many duplicates of each type I have. How is this done?

The array could look like this: @array = ('bent', 'svend', 'gert','pete','svend','pete');

Now I want the output like:

bent:1 gert:1 svend:2 pete:2

Thanks,
Rune

Replies are listed 'Best First'.
Re: Count duplicates in array.
by rdfield (Priest) on Nov 18, 2002 at 12:03 UTC
    Use a hash....
    my %hash; $hash{$_}++ foreach (@array); print "$_: $hash{$_} " foreach (sort {$hash{$a} <=> $hash{$b}} keys %h +ash);
    I'd have looked up a reference or two, but it's quicker to type in the code :)

    rdfield

Re: Count duplicates in array.
by BrowserUk (Patriarch) on Nov 18, 2002 at 12:30 UTC

    Or

    #! perl -slw use strict; my @array = ('bent', 'svend', 'gert','pete','svend','pete'); my ($temp, $count) = ("@array", 0); ($count = $temp =~ s/($_)//g) and printf "%2d:%s\n", $count, $_ for @a +rray; __END__ c:\test>213719 1:bent 2:svend 1:gert 2:pete c:\test>

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      It's working but i have some other need. My array is like this.

      @array=('SMSC_absentsubscriberSM','SMSC_absentsubscriberSM_SC_1','SMSC +_absentsubscriberSM_SC_2','SMSC_absentsubscriberSM',....and so on);

      Please suggest me to find exact duplicate in the array. Thanking you

Re: Count duplicates in array.
by RMGir (Prior) on Nov 18, 2002 at 12:50 UTC
    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

      Thanks for the many answers. It worked just fine.
      Br,
      Rune
Re: Count duplicates in array.
by UnderMine (Friar) on Nov 18, 2002 at 12:13 UTC
    First quick and dirty solution
    @array = ('bent', 'svend', 'gert','pete','svend','pete'); map($hash{$_}++, @array); print join(' ',map($_.':'.$hash{$_}, sort {$hash{$a}<=>$hash{$b}} keys + %hash));

    Hope it helps
    UnderMine