A Perl Solution to Lars' Problem

Share on FriendFeed
Posted by enrico
Thu, 07 Sep 2006 00:02:00 GMT

Stimulated by Marco, and by this post, I wanted to add my personal contribution…

Instead of jumping on the winner’s bandwagon like Marco, I opted for Perl, a language (once) de rigueur for problems like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my $numlines = $ARGV[0] or die "Missing parameter\n";
my %wcount; 

while (<STDIN>) {
        tr/A-Z/a-z/;
        for my $s (split /[^a-z0-9]+/) {
            next unless $s; 
            $wcount{$s}++;
    }
}

for my $w (sort { $wcount{$b} <=> $wcount{$a} } sort keys %wcount) {
    print $wcount{$w}, " $w\n";
    last unless --$numlines;
}
Comments

Leave a response

Comment