foreach $w ( ( sort { $count{$b} <=> $count{$a} } keys %count ) [0..4] ) { ... }