#!/usr/bin/perl use strict; use warnings; use constant WORD => 0; use constant LEN => 1; use constant NFORM => 2; use Inline C =>; my $file = $ARGV[0] || 'dictionary.txt'; open(my $fh, '<', $file) or die "Unable to open '$file' for reading: $!"; my @word; while (<$fh>) { $_ = lc; tr/a-z//cd; my %uniq = map {$_ => undef} split //; my $len = keys %uniq; push @word, [$_, $len, join '', sort keys %uniq]; } @word = sort {$b->[LEN] <=> $a->[LEN] } @word; for my $i (0 .. $#word - 1) { next if ! defined $word[$i]; for my $j ($i + 1 .. $#word) { next if ! defined $word[$j]; $word[$j] = undef if ! distinct($word[$i][NFORM], $word[$j][NFORM]); } } for (grep defined, @word) { print join "\t", $_->[NFORM], $_->[WORD]; print "\n"; } __END__ __C__ int distinct(unsigned char *str1, unsigned char *str2) { /* Actual code has 256 0s - truncated for post */ char exists[256] = {}; /* Turn array into a hash */ while (*str1) { exists[*str1++] = 1; } /* Determine if str2 contains any chars str1 does not */ while (*str2) { if (! exists[*str2++]) return 1; } return 0; }