jms53 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, I have a hash table from which I would like to print 20 keys with the highest values. I currently have this code:
my %top_words = reverse %words #reverse to sort by value as opposed to key %top_words = sort {$top_words{$b} <=> $top_words{$a}} %top_words; # so +rt z-a to get the highest ones at the top my $counter=0; foreach my $value (%top_words) { if ($counter > 20) {last;} #exit loop print "$top_words{$value}\n"; $counter++; } #print 20 words with highest value
This returns numbers, and the following error message:
Use of uninitialized value within %top_words in concatenation (.) or string at ./test.pl line 114. I can't see the error, and line 114 is the print command. Thank you.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Printing keys from a hash by value?
by Corion (Patriarch) on Jan 30, 2012 at 14:40 UTC | |
Re: Printing keys from a hash by value?
by ricDeez (Scribe) on Jan 30, 2012 at 15:17 UTC | |
by jms53 (Monk) on Jan 30, 2012 at 16:14 UTC | |
by ricDeez (Scribe) on Jan 30, 2012 at 15:55 UTC | |
Re: Printing keys from a hash by value?
by aaron_baugher (Curate) on Jan 30, 2012 at 15:58 UTC | |
by ricDeez (Scribe) on Jan 30, 2012 at 16:47 UTC | |
by aaron_baugher (Curate) on Jan 30, 2012 at 20:28 UTC | |
by jms53 (Monk) on Jan 30, 2012 at 16:38 UTC | |
by aaron_baugher (Curate) on Jan 30, 2012 at 20:53 UTC | |
by Anonymous Monk on Jan 30, 2012 at 19:49 UTC |
Back to
Seekers of Perl Wisdom