---------------------------
create an empty words_hash
while (input available)
read one word
windex = sorted lowercase word
skip word duplicates
add word to words_hash with key = windex
foreach windex in words_hash
remove unique words # = signatures with one word only
sort words_hash
foreach item in words_hash
print anagrams
----------------------------
####
Abby
abbot
acne
alert
alter
baby
Baby
BABY
best
bets
cane
later
Unix
UNIX
##
##
key values
----- ---------
abby => abby baby
abbot => abbot
acen => acne cane
best => best bets
inux => unix
aelrt => alert alter later
##
##
abby baby
acne cane
best bets
alert alter later
##
##
#!/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;
##
##
my @anagrams =();
foreach (values %words) {
push @anagrams, $_ if tr/ //;
# tr returns the number of processed characters,
# spaces in this case. Therefore, if spaces are > 0,
# it means that there are at least two words, and
# we have some anagrams. If space is 0, the word
# is unique and we skip it.
# grep returns all the items that satisfy the
# filtering condition (= are there any spaces?)
}
# map does the same as this foreach loop
foreach (sort @anagrams) {
print "$_\n"
}
##
##
if (exists $words{$windex}) {
my $word = $_;
next if grep {$_ eq $word} split / /,
$words{$windex} ; # skip duplicates
$words{$windex} .= " "; # add separating space
}
##
##
#!/usr/bin/perl -w
use strict;
my %words = ();
while (<>) {
chomp;
my $word = lc $_;
my $windex = pack "C*", sort unpack "C*", $word;
next if (exists $words{$windex})
and grep {$_ eq $word} @{$words{$windex}};
push @{$words{$windex}}, $word;
}
print map {"@$_\n"} sort grep {scalar @$_ > 1} values %words;
##
##
# French and Italian anagrams
my $windex = $_;
$windex =~ tr/àèéìòù[A-Z]/aeeiou[a-z]/; # gives the same signature
# to accented and
# unaccented vowels
tr/ÀÈÉÌÒÙ[A-Z]/àèéìòù[a-z]/; # converts to lowercase
$windex = pack "C*", sort unpack "C*", $windex; # create index
##
##
abolitionism mobilisation
acres cares races sacre scare
actualises casualties
aimless melissa
climaxes exclaim
collapse escallop
colonialist oscillation
detains instead sainted stained
donator odorant rotonda tornado
endoscopic picosecond
enumeration mountaineer
friary rarify
license selenic silence
obscurantist subtractions
predicts scripted
striptease tapestries
shower whores