#!/usr/bin/env perl # selfgrams.pl - find words that have valid anagrams # scruss - 2019-07 - may need wcanadian/wbritish/wamerican package # modified to use the insane British dictionary by ejolson use v5.20; my (@words, %grams, @elem, %output, $key, $fh); # create source word list from system file open( $fh, '<', '/usr/share/dict/british-english-insane' ) or die "$!\n"; chomp( @words = grep { /^[a-z]+$/ } <$fh> ); close($fh); # process source word list into hash of anagrams foreach (@words) { # sort word's letters to generate key - eg: 'maps' => 'amps' $key = join( '', sort( split( '', $_ ) ) ); # append word to existing hash match or create if word is new @elem = ( exists( $grams{$key} ) ) ? ( @{ $grams{$key} }, $_ ) : ($_); $grams{$key} = [@elem]; } # generate output hash of anagrams where key matched > 1 word foreach ( grep { scalar( @{$_} ) > 1 } values(%grams) ) { @elem = @{ $_ }; $key = shift(@elem); $output{$key} = join( ', ', @elem ); } foreach ( sort( keys(%output) ) ) { say join( ': ', $_, $output{$_} ); } exit;