#!/usr/local/bin/perl -w # wordhist - display histogram of words in input text # Andrew Ho (andrew@zeuscat.com) use strict; my %hash = (); my $maxlen = 0; my $maxcount = 0; while(<>) { chomp; tr/A-Z/a-z/; foreach(split) { $hash{$_}++; $maxlen = length if $maxlen < length; $maxcount = $hash{$_} if $maxcount < $hash{$_}; } } my $format = sprintf "%%-%ds %%%dd\n", $maxlen, length $maxcount; printf $format, $_, $hash{$_} foreach sort { $hash{$b} <=> $hash{$a} } keys %hash; exit 0;