riedquat - valueable resource for those who seek.
Home Blog Technical Reports Art Articles RapiDocs Coding Bugs Links Reviews Projects: CherBot Daimonin Gridarta

Cher's Perl Art

word histogram

This perl program counts the number of occurences of each word in texts. The art about this program of course lies in its brevity.

#!/usr/bin/perl
map $_{$_}++, split while <>;
print "$_: $_{$_}\n" for sort keys %_;

A slightly more sophisticated version could look like this:

#!/usr/bin/perl
map $_{$_}++, split /\b?\s+\b?|\b/ while <>;
print "$_: $_{$_}\n" for sort {$_{$b} <=> $_{$a}} keys %_;
 . 
..: