I am using the script below to find and replace while comparing variable names. Basically there are two sperate files where I extract a register name. When two registers match I need the register name to replaced with an old name. the problem with the script below is that it will do the replace and then print out the ENTIRE contents of the file but I only need to replace the contents and wait to print the contents once. anyone have any suggestions?
Code:
##### SAMPLE CODE
my $Adat_file = "prelim.dat";
my @registers = &read_dat_file($Adat_file); #new registers
my $Old_regs = "Compatible.txt";
my @Bregisters = &read_old_regs($Old_regs);
my $Acount = @Bregisters;
#change contents of old file
my $file = "prelim.dat";
#$file=@_;
my @contents =undef;
open(FILE, "+<$file") || die "Can't Find File: $file\n";
foreach my $register(@registers) { # loop through all the registers
my $Aname = $register->{name};
foreach my $Bregister(@Bregisters) {
my $Bnewname = $Bregister->{newname};
my $Boldname = $Bregister->{oldname};
my $Bprefix = $Bregister->{prefix};
if ($Aname eq $Bnewname) { # match is found
while($_=<FILE>) {
my $changes += s/R2_$Aname/$Bprefix_$Boldname/i;
push(@contents,$_);
next;
}
}
seek(FILE,0,0);
}
}
print FILE (@contents);
close(FILE);