Title: RE: Append on top

I looked at the archives and found a mail by NYMI Jose where the code was pretty neat. It was actually for inserting in the middle of the file. However, I could not get it to append on top. I am enclosing the mail containing his code as attachment if someone likes to comment.

Jenda, I like the approach proposed by you. But I've the same question Ajey asked (Why 8*1024)?

Rajesh

-----Original Message-----
From: Jenda Krynicky [mailto:[EMAIL PROTECTED]]
Sent: Friday, October 29, 2004 7:29 AM
To: [EMAIL PROTECTED]
Subject: RE: Append on top


From:                   [EMAIL PROTECTED]
> This is the way I would do it, however my code has been subject to
> criticism more that once in the past :)
>
> open (FILE, "<myfile.dat") or die;
> @file = <FILE>;
> close (FILE);
> $new_data = "This is my new line that I am going to put at the top\n";
> unshift (@file, $new_data); open (FILE, ">myfile.dat") or die; print
> FILE @file; close (FILE);

:-)

This is definitely a workable solution, there are just a few things
to keep in mind.

1)This would mean that the whole file is read into the memory.
2)This would force Perl to search for end-of-lines and create an
array.
3) You open the file in text mode in both cases. This may change the
type of newlines in the file!

So it's fine for small files, but not so great for bigger ones.

If you want something more efficient you might try something like
this:

open NEWFILE, '>', 'myfile.dat.tmp'
  or die "Can't create myfile.dat.tmp: $^E\n";
print NEWFILE "the new stuff to put on top\n";
...

open OLDFILE, '<', 'myfile.dat'
  or die "Can't open the original file myfile.dat: $^E\n";
binmode(OLDFILE);
binmode(NEWFILE);
my $buff;
print NEWFILE $buff
        while (read OLDFILE, $buff, 8*1024);
close NEWFILE;
close OLDFILE;
rename 'myfile.dat.tmp' => 'myfile.dat';

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


 

From: NYIMI Jose (BMB) [EMAIL PROTECTED]
Sent: Thursday, May 06, 2004 12:44 AM
To: [EMAIL PROTECTED]
Subject: RE: How do I append text to a text file - in the 3rd row ?

Does this helps ?

#!perl -w
use strict;

my $text="this is appended text";

while(<DATA>){
        print;
        print "$text\n" if $. == 3;
}

__DATA__
line1
line2
line3
line4
Line5


OUTPUT:

line1
line2
line3
this is appended text
line4
Line5

José.


**** DISCLAIMER ****

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> 
<http://learn.perl.org/first-response>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to