in reply to write exact lines of file

That is indeed a newbie/fa-question (this is a good tutorial). If you look around, you may find it. One of the answers is to use Tie::File.

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: write exact lines of file
by Abigail-II (Bishop) on Oct 08, 2003 at 09:56 UTC
    I wouldn't say that using Tie::File is a good advice for this particular problem. Tie::File is rather heavy-weight, in resource usage and time needed to learn its API. Tie::File pays back your investments by giving you some handy functionality, none of which you need for the problem at hand.

    It's like suggesting to use XS code to add two numbers. It's not impossible, it gives you lots of control and flexibility, but it doesn't make much sense.

    -n and $. are the solutions, not Tie::File.

    Abigail

Re: Re: write exact lines of file
by hash (Scribe) on Oct 09, 2003 at 17:05 UTC
    This way you can substitute a line i a text file by its number:
    use strict ; my $FILE = "file.txt"; open (FILE, "$FILE"); my @TOTAL = <FILE>; close FILE; $TOTAL[1] = "TEST\n"; my $FILE2 = "file2.txt"; open (FILE2, ">>$FILE2"); foreach (@TOTAL) { print FILE2 "$_"; } close FILE2;