my $myword;
my $wlist = "dict.txt";
my $strLen = length $myword;
open my $cfile, '<', $wlist or die "Can't open $wlist: $!\n";
while (defined (my $line = <$cfile>)) {
next if $strLen != length $line;
my @match;
my @used;
my $matchcount = 0;
for (my $i = 0; $i < $strLen; $i++) {
my $ch = substr $myword, $i, 1;
for (my $y = 0; $y < $strLen; ++$y) {
next if $ch ne substr $line, $y, 1;
if ($used[$y] eq $ch) {
$match[$matchcount] = 0;
} else {
$used[$y] = $ch;
$match[$matchcount] = 1;
$matchcount++;
last;
}
}
}
my $c = 0;
for (my $t = 0; $t < $strLen; $t++) {
++$c if ($match[$t]);
}
return $line if ($c == $strLen);
}
close $cfile;
This is of course completely untested and not what I would consider to be Perlish, although it is strict compliant. Note that the minor structure changes I made could actually be pushed back to the original C++ code to clean up that code somewhat.
True laziness is hard work
|