LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-11-2011, 08:08 PM   #1
vjx242
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Rep: Reputation: 0
Perl word Unscrambler seems to "pause" in middle.


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";	}
}
 
Old 07-11-2011, 09:29 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by vjx242 View Post
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.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 11:35 AM
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) d1s4st3r Programming 5 09-29-2010 10:30 PM
"Failed Dependency error" while installing RPM for "DateTime" perl modules giridhargopal.cj Linux - Newbie 7 11-19-2008 01:05 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 05:51 AM


All times are GMT -5. The time now is 11:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration