in reply to Counting events and getting top 5 matches
will work. This sorts the hash by value (in largest-first order), and uses a slice to pull off the top five.use strict; use warnings; my %code_count = ( a => 5, b => 4, c =>10, d => 1, e => 2, f => 17, ); print "$_ $code_count{$_}\n" for (sort { $code_count{$b} <=> $code_count{$a} } keys %code_count + )[0..4]; __DATA__ f 17 c 10 a 5 b 4 e 2
Updated: Added sample data and output.
|
|---|