Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reenter while loop

Status
Not open for further replies.

jess916

Programmer
Aug 26, 2008
11
0
0
US
How would I reenter a while loop within a while loop. So far my code reads a line from the first file and checks the first line of the second file to see if they are the same. If they do not match the next line of the 1st file is read, but I need the 2nd line of the first file to be read. Thus, reentering the 2nd while loop. My problem is that I use last when it doesn't find a match but the while loop is exited completely. My code is below. Any help woudl be greatly appreciated!
Code:
#!/usr/bin/perl

use strict 'vars';

my @regs =();
my @registers =();


open MYFILE2, "<", "blah.txt" or die $!;
open MYFILE1, "<", "compare.txt" or die $!;

my @lines = <MYFILE1>;
my @linetwo = <MYFILE2>;
my $line = undef;
my $linetwo = undef;
my $block = ();
my $reg = ();
my $register = ();
my $block2 = ();
	
	while (@lines) { 
		$line = shift(@lines);
		my $fields = undef;
		#Find first register match	
		if ($line =~ /^\s*Register\s*=\s*(\S+)/i) {
			$line = substr $1, 3;
			$reg->{name} = $line;
			#Enter next while loop to find find attributes
			while (@lines) {
		   		$line = shift(@lines);
				my $field = ();
				#find the number of entries
				if ($line =~ /^\s*Entries\s*=(\d)/i) {
					$line = $1;
					$reg->{entry} = $line;
				}
				#find the regsize byte
				if ($line=~ /^\s*RegSizeByte\s*=(\d)/i){
					$line = $1;
					$reg->{size} = $line;
				}
				#find offset to next entry
				if ($line=~ /^\s*OffsetToNextEntry\s*=(\dx\d)/i){
					$line = $1;
					$reg->{offset} = $line;
				}
				#find the number of subfields
				if ($line=~ /^\s*Subfields\s*=(\d+)/i){
					$line = $1;
					$reg->{subfield} = $line;
				}
				#find default value
				if ($line=~ /^\s*Default\s*=(\dx\S+)/i){
					$line = $1;
					$reg->{default} = $line;
				}
				#find register sub field, Bitoffset and size 
				if ($line=~ /^\s*RegisterSubField\s*=(\S+)\s+BitOffset\s*=(\d+)\s+Size\s*=(\d+)/i){ 
					my $subfield = $1;
					$line = $2;
					my $size = $3;
					$field->{RegisterSubField} = $subfield;
					$field->{bitoffset} = $line;
					$field->{size} = $size;
						push ( @{$reg->{fields}}, $field);
				}	
				#Find the next register name so the loop can be reentered 	 
				if ($line =~ /^\s*Register\s*=\s*(\S+)/i) {
					#Start reading second file to compare registers and attributes
					while (@linetwo) {
						my $field2 = undef;
						$linetwo = shift(@linetwo);
						#find register name from second file
						if ($linetwo =~ /^\s*Register\s*=\s*(\S+)/i) {
				
							$linetwo = substr $1, 4;
							$register->{name} = $linetwo;
						#enter the second while loop to find attributes 
						while (@linetwo) {
							$linetwo = shift(@linetwo);
							my $field2 = ();
							#Find the number of entries
							if ($linetwo =~ /^\s*Entries\s*=(\d)/i) {
								$linetwo = $1;
								$register->{entry} = $linetwo;
							}
							#find the register size bytes
							if ($linetwo =~ /^\s*RegSizeByte\s*=(\d)/i){
								$linetwo = $1;
								$register->{size} = $linetwo;
							}
							#find the offset to next entry
							if ($linetwo =~ /^\s*OffsetToNextEntry\s*=(\dx\d)/i){
								$linetwo = $1;
								$register->{offset} = $linetwo;
							}
							#find the number of subfields
							if ($linetwo =~ /^\s*Subfields\s*=(\d+)/i){
								$linetwo = $1;
								$register->{subfield} = $linetwo;

							}
							#find the default value	
							if ($linetwo =~ /^\s*Default\s*=(\dx\S+)/i){
							$linetwo = $1;
							$register->{default} = $linetwo;
							#print "$register->{default}\n";
							}
							#find attributes
							if ($linetwo =~ /^\s*RegisterSubField\s*=(\S+)\s+BitOffset\s*=(\d+)\s+Size\s*=(\d+)/i){ 
								my $subfield = $1;
								$linetwo = $2;
								my $size = $3;
								$field2->{RegisterSubField} = $subfield;
								$field2->{bitoffset} = $linetwo;
								$field2->{size} = $size;
								push ( @{$register->{fields}}, $field2);
								if ($register->{name} eq $reg->{name}) {
									print "$register->{name} matches $reg->{name}\n";
									print "test\n";
									last;
								}
							}
							#find when the next register is found to go back to beginning of loop
							if ($linetwo =~ /^\s*Register\s*=\s*(\S+)/i) {
								unshift (@lines, $line);
								push ( @{$block2->{registers}}, $register);
								last;
							}
						}
						}
					}		
				
				unshift (@lines, $line);
				#push @regs, \%reg;
				push ( @{$block->{registers}}, $reg);
				last;	
				}
			}
		}
	}	
	
		
close MYFILE1;
close MYFILE2;
 
Too lengthy to follow your logic without knowing anything on the structure of your data or what exactly you want to obtain.
Can you post a short piece of code, demonstrating only the problem you pose, and describe the interaction sought between the two files?

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
So far my code reads a line from the first file and checks the first line of the second file to see if they are the same. If they do not match the next line of the 1st file is read...
From what I can make out of your code, that isn't what it's doing at all. It seems to be scanning file1 for a Register=???, then parsing the file for a series of other name=value pairs until it reaches another Register= line. Only at that point does it start messing around with file2, presumably with a view to comparing the two files' values in some way. That's a deal more complex that the line-by-line comparison you claim to be doing.

I suggest you write a routine that parses a data file into, say, an array of hashes. Use this routine to process each file in turn, then loop through the resultant data structures to compare them.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Sorry, I should have explained more thouroughly. The code is looking for all the information i need for the first "block" of information. Then I need to go to the second file to see if it matches with a block in the second file. The code as is will compare the first register name from the first file with the first register name from the second file(I will worry about adding the other attributes later). But I need the while loop(which reads through the second file) to continue until it finds a match. Then the second "block" of information in the first file will be retained and so on. Below is some sample data. Test_123 would be a match but those two never get compared with my code. Only the Sample_1 and Test_123 get compared.

Sample Data

First File Second File
register=TEST_123 register=SAMPLE_1
entry=5 entry=7
... ...
register=blah register=TEST_123
entry=1 entry=5
.... ...
 
Follow Chris's advice, reading first the second file into a hash structure, instead of a simple array of lines, then parse the first one.
Otherwise you need to do a [tt]seek MYFILE2,0,0;[/tt] each time you go to the second file to rewind it.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top