Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a perl script which would open and edit an html file which looks like this:
<tr> <td WIDTH="16%" align="center"><b><a href="A01/A01.htm">Alpha ims10wk3 +3f</a></b></td> <td WIDTH="16%" align="center"><b><font face="Arial Black" color="#00F +F00"><b>GREEN</b></font></td> </tr> <tr> <td WIDTH="16%" align="center"><b><a href="A02/A02.htm">Beta ims10wk33 +ff</a></b></td> <td WIDTH="16%" align="center"><b><font face="Arial Black" color="#00F +F00"><b>GREEN</b></font></td> </tr> <tr> <td WIDTH="16%" align="center"><b><a href="A2/A2.htm">ims10wk33b</a></ +b></td> <td WIDTH="16%" align="center"> </tr>
I have to delete the first row of the table i.e. everything that comes between the first tr tags and add similar text at the last row of the table. Hence the perl script has to delete whatever comes between the first tr tags and add some text after the last tr tag. The actual html file is much longer than what is shown above. Could anyone help me on this? Thanks!

Replies are listed 'Best First'.
Re: How to delete and add line in the middle of a file?
by dragonchild (Archbishop) on Oct 01, 2001 at 23:20 UTC
    1. Read in the whole file using the form my @fileArray = <IN_FILE>;
    2. Manipulate said array to make it look like you want.
    3. Write the array back out to the file using the form print OUT_FILE @fileArray;

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: How to delete and add line in the middle of a file?
by Ido (Hermit) on Oct 02, 2001 at 03:58 UTC
    Reading the file into an array might be a big mistake, instead, use the in-place editing:
    local ($^I,@ARGV)=('.bak','file.html'); my $p=1; while(<>){ $p-- if /<\/tr>/; print "Add this line\n" if $p==-4 print if $p; $p-- if /<tr>/; }
    You can read more about in-place editing in perlvar($^I) and perlrun(-i).
Re: How to delete and add line in the middle of a file?
by mischief (Hermit) on Oct 02, 2001 at 18:05 UTC