in reply to Top n Unique count from Array

How about this?

use Modern::Perl; my %seen; while( <DATA> ) { chomp; $seen{$_}++; } my @top = ( sort { $seen{$b} <=> $seen{$a} } keys %seen )[0,1]; foreach ( @top ) { say "$_ $seen{$_}"; } __DATA__ 1.1.1.1 2.2.2.2 1.1.1.1 4.4.4.4 1.1.1.1 4.4.4.4

Dave

Replies are listed 'Best First'.
Re^2: Top n Unique count from Array
by cipher (Acolyte) on Mar 25, 2011 at 08:25 UTC
    UPDATE: Looks like I found the solution. This one is working for me:
    my $top = 10; my %cnt_hash; $cnt_hash{$_}++ foreach @srcs; foreach $key (sort {$cnt_hash{$b} <=> $cnt_hash{$a}} keys %cnt_hash) { print "$key\t$cnt_hash{$key}\n"; $i++; if ($i == $top){last;} }