what is wrong with below?
I was going through tutorial in perl.com and first I think it has typo where it compares b to b (where it should be a to b and b to a) but also when I run the code itself, it does NOT sort it correctly..
Can you please tell me why this code does not sort it correctly?
http://www.perl.com/pub/a/2006/11/02/all-about-hashes.html
#!/usr/bin/perl -w
use strict;
my @list = qw(oranges oranges apple tomato tomato tomato tomato waterm
+elon watermelon watermelon);
my %histogram;
$histogram{$_}++ for @list;
my @unique = keys %histogram;
foreach (keys %histogram) {
print "$_ , $histogram{$_}\n";
}
my @popular = (sort { $histogram{$b} <=> $histogram{$a} } @unique)[0.
+.2];
print "@popular\n";
__END__
foreach (keys %histogram) {
print "$_ , $histogram{$_}\n";
}
[root@myserver chaos]# ./!$
./perl.hashcrashcourse
oranges , 2
watermelon , 3
tomato , 4
apple , 1
tomato watermelon oranges
#!/usr/bin/perl -w
use strict;
my @list = qw(oranges oranges apple tomato tomato tomato tomato waterm
+elon watermelon watermelon);
my %histogram;
$histogram{$_}++ for @list;
my @unique = keys %histogram;
foreach (keys %histogram) {
print "$_ , $histogram{$_}\n";
}
my @popular = (sort { $histogram{$a} <=> $histogram{$b} } @unique)[0.
+.2];
print "@popular\n";
__END__
foreach (keys %histogram) {
print "$_ , $histogram{$_}\n";
}
[root@myserver chaos]# ./!$
././perl.hashcrashcourse
oranges , 2
watermelon , 3
tomato , 4
apple , 1
apple oranges watermelon
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.