#!/usr/bin/perl -w # usage: anagram.pl < wordlist use strict; my %words = (); # words index (Key: sorted # word. Value: word(s)) while (<>) { chomp; # remove newline character $_ = lc $_; # converts to lowercase my $windex = pack "C*", sort unpack "C*", $_; # create index if (exists $words{$windex}) { next if $words{$windex} =~ /\b$_\b/; # skip duplicates $words{$windex} .= " "; # add separating space } $words{$windex} .= $_; # inserts a new word } print map {"$_\n"} sort grep {tr/ //} values %words;