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;