convenientstore has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|