develooper Front page | perl.beginners | Postings from March 2002

How to insert a line in the middle of a file?

Thread Next
From:
jkypa
Date:
March 4, 2002 11:11
Subject:
How to insert a line in the middle of a file?
Message ID:
200203041911.g24JBST24507@dymwsm10.mailwatch.com
Hi,
	I am modifying an existing perl script to basically insert a statement
as the 3rd sentence of a file(so the existing 3rd sentence of the file
would become the 4th sentence). I've looked at some FAQs on the
perldoc.com site and managed to get started. However, I am only able to
insert this statement at the top of the file and then everything else
within the file is overwritten. 
	I am pasting the segment of the perl code that I added. The statement
that I want to insert is the "Integrator" statement. This might be very
easy for most of you. I am not a programmer by any standard and never
used Perl. So, thanks in advance for your help. 	
	
	My code goes like this.....

$old = "$LeafPreName.acf";
$new = "$old".".tmp";
$bak = "$old".".orig";

open(OLD, "<$old");
open(NEW, ">$new");

while (<>) {
   if ($. == 3) {
   print NEW "INTEGRATOR/ ERROR=5.0E-2, HINIT=2.5E-4,HMAX=2.5E-4,
KMAX=6, MAXIT=10\n";
                }
}
close(OLD);
close(NEW);

rename($old,$bak);
rename($new,$old);

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About