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