| Sergei Steshenko |
07-11-2011 08:29 PM |
Quote:
Originally Posted by vjx242
(Post 4412087)
Hey guys, I created the script to unscramble words from a file. It works fine but it seems to have stopped or pause after it found 7 matches instead of continuing to 10. There are 10 scrambled words and over 1000 unscrambled words to compare them to. Can anybody figure out why? Thank you!
Code:
#!/usr/local/bin/perl
#use strict; #always add this in the beginning of code.
#use warnings; #always add this in the beginning of code.
#open(INFILE, '<', 'sample.txt'); #For reading
#open(INFILE, '>', 'sample.txt'); #For writing
#open(INFILE, '>>', 'sample.txt'); #To append
my $TheInput = 'input.txt'; #File where you paste the scrambled words
my $TheWordList = 'wordlist.txt'; #File that contains the list of words that are unscrambled
my $TheOutput = 'output.txt'; #File where the matched words will be pasted
my $i = 0;
readinputlist(); #read scrambled list of words from a file
readwordlist(); #read unscrambled list from file
comparison(); #check the words to see if they match, if so save them in the array
outputInFile(); #paste the words into a file
sub readinputlist
{
@scrambledWholeWord = ();
my $i = 0;
open(THEINPUT, '<', $TheInput) or die "\nERROR: Can't open $TheInput \n";
while (<THEINPUT>)
{
my $line = $_;
chomp($line);
$scrambledWholeWord[$i] = $line;
$i+=1;
}
return @scrambledWholeWord;
close(THEINPUT);
}
sub readwordlist
{
@unscrambledWholeWord = ();
my $i = 0;
open(THEWORDLIST, '<', $TheWordList) or die "\nERROR: Can't open $TheWordList \n";
while (<THEWORDLIST>)
{
my $eachLine = $_; #the line that is currently being examined
chomp($eachLine);
$unscrambledWholeWord[$i] = $eachLine; #word from wordlist file
$i+=1;
}
return @unscrambledWholeWord;
close(THEWORDLIST);
}
sub comparison
{
my $scrambledIndex = 0; #an index of scrambled list
my $unscrambledIndex = 0; #an index of unscrambled list
my $j = 0; #keep an index of the unscrambled words
my $k = 0;
my @scrambledWord;
my @thisUnscrambledWord;
my $numberOfLettersScrambled;
my $numberOfLettersUncrambled;
my $matchCount = 0; #keep a count of how many matches are found.
@matchedWords = (); #list to contain words to be printed (matched words)
my $y = 0;
$endOfArray = $#unscrambledWholeWord;
while ($#matchedWords+1 < 10)
{
@scrambledWord = split(//, $scrambledWholeWord[$k]);
@thisUnscrambledWord = split(//, $unscrambledWholeWord[$j]); #split the letters of the word up into an array
$numberOfLettersScrambled = $#scrambledWord; #number of letters in the list scrambledWord
$numberOfLettersUnscrambled = $#thisUnscrambledWord; #number of letters in the list scrambledWord
$scrambledIndexSaved = $scrambledIndex; #save the index of the scrambled word
$unscrambledIndexSaved = $unscrambledIndex; #save the index of the unscrambled word
#print "$scrambledWholeWord[$k]\n";
#print "$unscrambledWholeWord[$j]\n";
#print "$numberOfLettersScrambled\n";
#print "$numberOfLettersUnscrambled\n";
#print "$scrambledWord[$scrambledIndex]\n";
#print "$thisUnscrambledWord[$unscrambledIndex]\n";
if ($thisUnscrambledWord[$unscrambledIndex] eq $scrambledWord[$scrambledIndex])
{$matchCount++;}
$scrambledIndex++;
if (($matchCount == $numberOfLettersUnscrambled)&&($matchCount == $numberOfLettersScrambled))
{
print "MATCH FOUND! $scrambledWholeWord[$k]\n";
$matchedWords[$y] = $unscrambledWholeWord[$j];
$y++;
$j = 0;
$k++;
$unscrambledIndex = 0;
$scrambledIndex = 0;
$matchCount = 0;
}
if ($scrambledIndex == $numberOfLettersScrambled)
{
$scrambledIndex = 0;
$unscrambledIndex++;
}
if ($unscrambledIndex == $numberOfLettersUnscrambled)
{
$unscrambledIndex = 0;
$j++;
$matchCount = 0;
$wordsTried++;
}
if ($wordsTried == $endOfArray)
{
$wordsTried = 0;
$k++;
}
}
return @matchedWords;
}
sub outputInFile
{
my $i = 0;
open(OUT, '>output.txt') or die "\nError: Couldn't open $ThisFile \n";
for ($i = 0; $i<10; $i+=1)
{ print "$matchedWords[$i]\n"; }
}
|
First of all, uncomment
Code:
#use strict; #always add this in the beginning of code.
#use warnings; #always add this in the beginning of code.
- I do not think you have any reasons to ignore compiler error messages and runtime warnings.
Then make you code compile and run with zero error/warning messages.
Then it will make sense to look in more detail into your code.
If you think the compiler and the runtime warnings/error messages are wrong and you are right, file bugs against Perl and at all become a Perl language developer.
|