Try this with your dictionary file. If performance is a problem break into separate files according to word length.
#!perl
use strict;
open IN,'dict.txt' or die "$!";
my %dict=();
for (<IN>){
chomp;
$dict{uc $_}=1;
};
close IN;
for my $word (sort keys %dict){
next unless $word =~ /R/;
my @f = split //,$word;
# loop over each letter
# changing R to S
# to create new word in $w
for my $i (0..$#f){
if ($f[$i] eq 'R'){
my $w = $word;
substr($w,$i,1) = "S";
# check if generated word exists in dict
if (exists $dict{$w}){
print "$word $w\n";
}
}
}
}
poj
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|