Re: Append on top (easy sollution)

2004-11-16 Thread K-sPecial
K-sPecial wrote:
Rajesh Dorairajan wrote:
Does anyone know of a way to open a file in append mode and append on 
top of
the file?
Thanks in Advance,

--Rajesh

I don't know why people are having a problem with this sollution, simply 
open in append mode so open doesn't clobber the file, then use seek() to 
I see now, damned my big mouth :-p
--K-sPecial


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: Append on top (easy sollution)

2004-11-15 Thread Ron Goral


> -Original Message-
> From: Gary Stainburn [mailto:[EMAIL PROTECTED]
> Sent: Monday, November 15, 2004 7:25 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Append on top (easy sollution)
>
>
> On Monday 15 November 2004 3:02 am, K-sPecial wrote:
> > Rajesh Dorairajan wrote:
> > > Does anyone know of a way to open a file in append mode and append
> > > on top of the file?
> > >
> > > Thanks in Advance,
> > >
> > > --Rajesh
> >
> > I don't know why people are having a problem with this sollution,
> > simply open in append mode so open doesn't clobber the file, then use
> > seek() to move to the beginning of the file. Done :)
> >
>
> The problem is that if you do that, you're going to overwrite the
> existing data with every write you do. What is required when appending
> to the front (oxymoron as append means to stick on the end), is that
> the existing data is shuffled down when you write.
>
> To clarify.
>
> if you had
>
> a
> b
> c
> d
>
> and did as you suggested to write 'f' you would end up with
>
> f
> b
> c
> d
>
> See?
>
> > --K-sPecial
> >
> > [ http://xzziroz.freeshell.org
> >irc://xzziroz.dtdns.net   ]
>
> --
> Gary Stainburn
>
> This email does not contain private or confidential material as it
> may be snooped on by interested government parties for unknown
> and undisclosed purposes - Regulation of Investigatory Powers
> Act, 2000

Perl FAQ 5 talks about this in the subsection entitled "How do I change one
line in a file/delete a line in a file/insert a line in the middle of a
file/append to the beginning of a file?"

Peace -
Ron Goral



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




Re: Append on top

2004-11-15 Thread Gary Stainburn
On Monday 15 November 2004 1:43 pm, NYIMI Jose \(BMB\) wrote:
> > However, if you do have these restrictions it can be done using the
> > psudocode below
> >
> > calculate write_size
> > seek EOF-buffer_size
> > read buffer_size
> > seek EOF-buffer_size+write_size
> > repeat until at BOF
Don't forget

Seek BOF
write new data
>
> Why should I care with all these details when Tie::File is available
> from CPAN. And now comes as standard dist of recent Perl version ?
>
> José.
>
Hi José.

You man want to know this, because, as I said in my OP there may be 
reasons why you can't create a new file and have to do the update in 
place.  Tie::File, like the other methods creates a new file and then 
copies that over the original.

Gary
-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Append on top

2004-11-15 Thread Shaw, Matthew
> Rajesh Dorairajan wrote:
> > Does anyone know of a way to open a file in append mode and append
on top of
>
> I don't know why people are having a problem with this sollution,
simply 
> open in append mode so open doesn't clobber the file, then use seek()
to 
> move to the beginning of the file. Done :)

Not quite so done -- this solution just plain doesn't work. When I want
to prepend text to the beginning of a file, I use perl's CLI edit in
place like this:

perl -ni.bak -we'print "Some next text here\n" if $. == 1; print;'
filetoedit.txt

Or from within a program:

push @ARGV, 'foobar.txt';
$^I = '.bak';
while (<>) {
print "Some new text here.\n" if $. == 1;
print; # print $_
}

Or, as suggested in the FAQ, Tie::File is good for stuff like this:

Use Tie::File;

my $file = 'textfile.txt';
my @tied;

tie @tied, 'Tie::File', $file or die $!;

unshift @tied, 'My new PRE-pended line!';
untie @tied;

None of these things actually 'edit the file in place', they re-write
the file. Hope this helps.

See also: perldoc -q append


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Append on top

2004-11-15 Thread NYIMI Jose \(BMB\)


> -Original Message-
> From: Gary Stainburn [mailto:[EMAIL PROTECTED] 
> Sent: Monday, November 15, 2004 2:25 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Append on top
> 
> 
> On Friday 29 October 2004 10:35 am, Ing. Branislav Gerzo wrote:
> > Rajesh Dorairajan [RD], on Thursday, October 28, 2004 at 18:11
> > (-0700) thoughtfully wrote the following:
> >
> > RD> Does anyone know of a way to open a file in append mode and
> > append on top of RD> the file?
> >
> > I don't think it is possible. You have to read source file, 
> and after 
> > print your results to new file (open through >) and append old file 
> > through >>
> 
> It is possible, but very messy.  The above method is the most 
> efficient, 
> but does require free disk space equivelent to the the size 
> of the new 
> file.  Not usually a problem these days, withthe low cost of HDDs.  
> 
> However, if you do have these restrictions it can be done using the 
> psudocode below
> 
> calculate write_size
> seek EOF-buffer_size
> read buffer_size
> seek EOF-buffer_size+write_size
> repeat until at BOF
> 

Why should I care with all these details when Tie::File is available from CPAN.
And now comes as standard dist of recent Perl version ?

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. This email is not intended to 
create or affect any contractual arrangements between the parties.
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>




Re: Append on top

2004-11-15 Thread Gary Stainburn
On Monday 15 November 2004 1:25 pm, Gary Stainburn wrote:
[snip]
>
> calculate write_size
> seek EOF-buffer_size
> read buffer_size
> seek EOF-buffer_size+write_size
> repeat until at BOF
>

Oops, missed out the final lines of code, which are:

seek BOF
write new data

bit of a pointless excercise without that.

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Append on top

2004-11-15 Thread Gary Stainburn
On Friday 29 October 2004 10:35 am, Ing. Branislav Gerzo wrote:
> Rajesh Dorairajan [RD], on Thursday, October 28, 2004 at 18:11
> (-0700) thoughtfully wrote the following:
>
> RD> Does anyone know of a way to open a file in append mode and
> append on top of RD> the file?
>
> I don't think it is possible. You have to read source file, and after
> print your results to new file (open through >) and append old file
> through >>

It is possible, but very messy.  The above method is the most efficient, 
but does require free disk space equivelent to the the size of the new 
file.  Not usually a problem these days, withthe low cost of HDDs.  

However, if you do have these restrictions it can be done using the 
psudocode below

calculate write_size
seek EOF-buffer_size
read buffer_size
seek EOF-buffer_size+write_size
repeat until at BOF

Actually coding this into perl should be fairly easy, and could even be 
used to insert data mid-file if required.

However, if this is something that you're going to be doing regularly, 
I'd suggest re-evaluating the data format, such as using a database.

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Append on top (easy sollution)

2004-11-15 Thread Gary Stainburn
On Monday 15 November 2004 3:02 am, K-sPecial wrote:
> Rajesh Dorairajan wrote:
> > Does anyone know of a way to open a file in append mode and append
> > on top of the file?
> >
> > Thanks in Advance,
> >
> > --Rajesh
>
> I don't know why people are having a problem with this sollution,
> simply open in append mode so open doesn't clobber the file, then use
> seek() to move to the beginning of the file. Done :)
>

The problem is that if you do that, you're going to overwrite the 
existing data with every write you do. What is required when appending 
to the front (oxymoron as append means to stick on the end), is that 
the existing data is shuffled down when you write.

To clarify.

if you had 

a
b
c
d

and did as you suggested to write 'f' you would end up with

f
b
c
d

See?

> --K-sPecial
>
> [ http://xzziroz.freeshell.org
>irc://xzziroz.dtdns.net   ]

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Append on top (easy sollution)

2004-11-15 Thread Jenda Krynicky
From: K-sPecial <[EMAIL PROTECTED]>
> Rajesh Dorairajan wrote:
> > Does anyone know of a way to open a file in append mode and append
> > on top of the file? 
> > 
> > Thanks in Advance,
> > 
> > --Rajesh 
> > 
> 
> I don't know why people are having a problem with this sollution,
> simply open in append mode so open doesn't clobber the file, then use
> seek() to move to the beginning of the file. Done :)

Could be ... because it doesn't work?

Try this:

#!perl
use Fcntl qw(SEEK_SET);
open OUT, '>', 'testAppend.txt' or die "Can't create testAppend.txt: 
$^E\n";
print OUT "Original content\nThis is the old stuff in the file you 
are trying to append to.\n";
close OUT;

print "--original content--\n";
open IN, '<', 'testAppend.txt';
while () {print}
close IN;
print "\n";

open APPEND, '>>', 'testAppend.txt' or die "Can't append to 
testAppend.txt: $^E\n";
seek APPEND, 0, SEEK_SET;
print APPEND "--K-sPecial says this will go on top.\n";
close APPEND;

print "--updated  content--\n";
open IN, '<', 'testAppend.txt';
while () {print}
close IN;
print "\n";
__END__

I get:


--original content--
Original content
This is the old stuff in the file you are trying to append to.

--updated  content--
Original content
This is the old stuff in the file you are trying to append to.
--K-sPecial says this will go on top.


Doesn't look like anything was "appended on top".

Think of a file like it was a sheet of paper. You may add more text 
after the end of the current one easily, you can erase part of the 
text and change it to a different one if the new text is as long as 
the old one or shorter. And if it's shorter you end up with some 
whitespace. But you can't squeeze more text on top or in the middle, 
you'd have to erase everything and write first the new text and then 
the original one.

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]
 




Re: Append on top (easy sollution)

2004-11-14 Thread K-sPecial
Rajesh Dorairajan wrote:
Does anyone know of a way to open a file in append mode and append on top of
the file? 

Thanks in Advance,
--Rajesh 

I don't know why people are having a problem with this sollution, simply 
open in append mode so open doesn't clobber the file, then use seek() to 
move to the beginning of the file. Done :)

--K-sPecial
[ http://xzziroz.freeshell.org
  irc://xzziroz.dtdns.net   ]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Append on top

2004-11-01 Thread Chasecreek Systemhouse
On Tue, 02 Nov 2004 01:10:40 +0100, Jenda Krynicky <[EMAIL PROTECTED]> wrote:

> It seems though that the constant doesn't matter much. I've tried to
> copy a 709MB file using a tiny script that looked like this:
...
> and it took 171s with 8*1024 and 176 with 5000 and 32*1024 byte
> chunks.
> 
> OTOH converting it to sysopen/sysread/syswrite made a big difference.
> THe time went down to 79s with 32*1024 byte chunks but 180-185s with
> 5000 byte ones.


well, you are doing the OpSys work -- more or less -- for it.

However, bug question is -- which method was more system freindly?   
or was this project copy simply a once-in-while thing you wanted to
get done quickly?

No matter what the system load performance cost was?

-- 
WC -Sx- Jones
http://youve-reached-the.endoftheinternet.org/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Append on top

2004-11-01 Thread Jenda Krynicky
From: Chasecreek Systemhouse <[EMAIL PROTECTED]>
> On Sun, 31 Oct 2004 22:04:33 +0100, Jenda Krynicky <[EMAIL PROTECTED]>
> wrote:
> 
> > Something like that. 1024 is one K. So 8*1024 is 8KB.
> > 
> > I don't know what the size of a block is when reading from the
> > particular disk storing that file, but the block size is usualy some
> > power of 2 in KB. 2KB, 4KB, 8KB, 16KB, 32KB. It would of course be
> > most efficient if I was reading the file in chunks equal to the
> > block size, but 8KB seems to me like a reasonable guess.
> 
> 
> PMFJI  =)
> 
> I would like to think that high-level programmers simply read and
> write $strings, @arrays, etc, and allow the under-lying OpSys (XP,
> Solaris, FC3, etc) to worry about the file access methods.
> 
> Operating systems can access data at whatever speed the filesystem
> format (Ext2, Ext3, Reiser, Quorums, Raids, Fly-swatter) would allow
> without people generally having to worry about sectors, blocks, etc.

That's why I would not sweat about this too much. But if I can make 
the computer's work easier just by choosing a good constant I will.

It seems though that the constant doesn't matter much. I've tried to 
copy a 709MB file using a tiny script that looked like this:

open IN, '<', 'Kour.avi';
open OUT, '>', 'Kour.avi.out';
binmode(IN);binmode(OUT);

my $time = time();
my $buff;
while(read IN, $buff, 32*1024) {
print OUT $buff;
}

print "Done in " . (time() - $time) . " seconds\n";

and it took 171s with 8*1024 and 176 with 5000 and 32*1024 byte 
chunks.

OTOH converting it to sysopen/sysread/syswrite made a big difference. 
THe time went down to 79s with 32*1024 byte chunks but 180-185s with 
5000 byte ones.

use Fcntl;
sysopen IN, 'Kour.avi', O_RDONLY;
sysopen OUT, 'Kour.avi.out', O_WRONLY | O_CREAT;

my $time = time();
my $buff;
while(sysread IN, $buff, 32*1024) {
syswrite(OUT, $buff);
}

print "Done in " . (time() - $time) . " seconds\n";

Of course under normal circumstances you have much better things to 
worry about than what chunksize to use ;-)

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]
 




Re: Append on top

2004-11-01 Thread Chasecreek Systemhouse
On Sun, 31 Oct 2004 22:04:33 +0100, Jenda Krynicky <[EMAIL PROTECTED]> wrote:

> Something like that. 1024 is one K. So 8*1024 is 8KB.
> 
> I don't know what the size of a block is when reading from the
> particular disk storing that file, but the block size is usualy some
> power of 2 in KB. 2KB, 4KB, 8KB, 16KB, 32KB. It would of course be
> most efficient if I was reading the file in chunks equal to the block
> size, but 8KB seems to me like a reasonable guess.


PMFJI  =)

I would like to think that high-level programmers simply read and
write $strings, @arrays, etc, and allow the under-lying OpSys (XP,
Solaris, FC3, etc) to worry about the file access methods.

Operating systems can access data at whatever speed the filesystem
format (Ext2, Ext3, Reiser, Quorums, Raids, Fly-swatter) would allow
without people generally having to worry about sectors, blocks, etc.

If the data you want, say on a Tape, or is on another "truly" blocked
device -- then maybe you can gain speed by worrying about low-level
things.

Historically, really fast disks and a LOT of RAM equal fast data processing.

-- 
WC -Sx- Jones
http://youve-reached-the.endoftheinternet.org/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Append on top

2004-10-31 Thread Jenda Krynicky
From: Ajey Kulkarni <[EMAIL PROTECTED]>
> >>> while (read OLDFILE, $buff, 8*1024);
> 
> A quick qn. :) Y is 8*1024 being mentioned here? Are you just
> word-aligning or something else?

Something like that. 1024 is one K. So 8*1024 is 8KB.

I don't know what the size of a block is when reading from the 
particular disk storing that file, but the block size is usualy some 
power of 2 in KB. 2KB, 4KB, 8KB, 16KB, 32KB. It would of course be 
most efficient if I was reading the file in chunks equal to the block 
size, but 8KB seems to me like a reasonable guess. 

I'm sorry I don't remember the right names, I'm sure someone can 
explain better.

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]
 




Re: Append on top

2004-10-29 Thread John W. Krahn
Jenda Krynicky wrote:
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, ";
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';
You *can* of course modify the file in-place (if the OS supports it.)
my $file = 'myfile.dat';
my $text = "the new stuff to put on top\n";
open my $IN, '+<', $file or die "Cannot open '$file' for input: $!";
binmode $IN;
open my $OUT, '+<', $file or die "Cannot open '$file' for output: $!";
binmode $OUT;
$/ = \length $text;
while ( my $buff = <$IN> ) {
print $OUT $text;
$text = $buff;
print $OUT $text if eof $IN;
}

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: Append on top

2004-10-29 Thread Rajesh Dorairajan
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, "
> @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(){
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>


RE: Append on top

2004-10-29 Thread Ajey Kulkarni


>>> while (read OLDFILE, $buff, 8*1024);

A quick qn. :) Y is 8*1024 being mentioned here? Are you just
word-aligning or something else?

regards
-Ajey

On Fri, 29 Oct 2004, Jenda Krynicky
wrote:


> 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, " > @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]
>  
>
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Append on top

2004-10-29 Thread Jenda Krynicky
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, " @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]
 




RE: Append on top

2004-10-29 Thread brian . barto
This is the way I would do it, however my code has been subject to criticism
more that once in the past :)

open (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);

-Original Message-
From: NYIMI Jose (BMB) [mailto:[EMAIL PROTECTED]
Sent: Friday, October 29, 2004 9:34 AM
To: Mandar Rahurkar; Rajesh Dorairajan; [EMAIL PROTECTED]
Subject: RE: Append on top




> -Original Message-
> From: Mandar Rahurkar [mailto:[EMAIL PROTECTED] 
> Sent: Friday, October 29, 2004 3:18 PM
> To: Rajesh Dorairajan; Perl-Beginners ([EMAIL PROTECTED])
> Subject: Re: Append on top
> 
> 
> read the file. Rewrite ur data into the file followed by 
> original data. easy way..
> 

I'm not convinced this approach will do the job.
Please send an excerpt code ...

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. This email is not
intended to create or affect any contractual arrangements between the
parties.
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>




RE: Append on top

2004-10-29 Thread NYIMI Jose \(BMB\)


> -Original Message-
> From: Mandar Rahurkar [mailto:[EMAIL PROTECTED] 
> Sent: Friday, October 29, 2004 3:18 PM
> To: Rajesh Dorairajan; Perl-Beginners ([EMAIL PROTECTED])
> Subject: Re: Append on top
> 
> 
> read the file. Rewrite ur data into the file followed by 
> original data. easy way..
> 

I'm not convinced this approach will do the job.
Please send an excerpt code ...

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. This email is not intended to create or affect any 
contractual arrangements between the parties.
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>




Re: Append on top

2004-10-29 Thread Mandar Rahurkar
read the file. Rewrite ur data into the file followed by original data. easy way..

Mandar

 Original message 
>Date: Thu, 28 Oct 2004 18:11:54 -0700
>From: "Rajesh Dorairajan" <[EMAIL PROTECTED]>  
>Subject: Append on top  
>To: "Perl-Beginners ([EMAIL PROTECTED])" <[EMAIL PROTECTED]>
>Cc: "Rajesh Dorairajan" <[EMAIL PROTECTED]>
>
>Does anyone know of a way to open a file in append mode and append on top of
>the file? 
>
>Thanks in Advance,
>
>--Rajesh 
---
Mandar Rahurkar
ECE,   UIUC
---

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Append on top

2004-10-29 Thread Bob Showalter
Rajesh Dorairajan wrote:
> Does anyone know of a way to open a file in append mode and append on
> top of the file?

No, because that's not what "append mode" means. To insert data anywhere but
at the end, you have to rewrite the file contents from the point of
insertion forward. Or, you need to write the new data plus the old file
contents to a temporary file and then move it back over the old file.

Jose's Tie::File suggestion is probably the easiest way to go.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Append on top

2004-10-29 Thread NYIMI Jose \(BMB\)


> -Original Message-
> From: Ing. Branislav Gerzo [mailto:[EMAIL PROTECTED] 
> Sent: Friday, October 29, 2004 11:36 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Append on top
> 
> 
> Rajesh Dorairajan [RD], on Thursday, October 28, 2004 at 
> 18:11 (-0700) thoughtfully wrote the following:
> 
> RD> Does anyone know of a way to open a file in append mode 
> and append 
> RD> on top of the file?
> 
> I don't think it is possible. You have to read source file, 
> and after print your results to new file (open through >) and 
> append old file through >>

Or no need to open the file:

use Tie::File;
tie @array, 'Tie::File', filename or die "...";
unshift @array, "new recs"; #will append on top
untie @array;

HTH,
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. This email is not intended to create or affect any 
contractual arrangements between the parties.
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>




Re: Append on top

2004-10-29 Thread Ing. Branislav Gerzo
Rajesh Dorairajan [RD], on Thursday, October 28, 2004 at 18:11 (-0700)
thoughtfully wrote the following:

RD> Does anyone know of a way to open a file in append mode and append on top of
RD> the file?

I don't think it is possible. You have to read source file, and after
print your results to new file (open through >) and append old file
through >>

-- 

 ...m8s, cu l8r, Brano.

[Confucius Says: Man who has woman on ground has piece on earth!]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Append on top

2004-10-29 Thread arjun.mallik

This mode will append text at the bottom not at the top.

Arjun


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
Sent: Friday, October 29, 2004 2:45 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: RE: Append on top


open(MYNAME,">>a.txt");


-Original Message-
From: Rajesh Dorairajan [mailto:[EMAIL PROTECTED]
Sent: 29 October 2004 02:12
To: Perl-Beginners ([EMAIL PROTECTED])
Cc: Rajesh Dorairajan
Subject: Append on top


Does anyone know of a way to open a file in append mode and append on
top of the file?

Thanks in Advance,

--Rajesh

Visit our website at http://www.ubs.com

This message contains confidential information and is intended only
for the individual named.  If you are not the named addressee you
should not disseminate, distribute or copy this e-mail.  Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses.  The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission.  If
verification is required please request a hard-copy version.  This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.


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





Confidentiality Notice

The information contained in this electronic message and any attachments to this 
message are intended
for the exclusive use of the addressee(s) and may contain confidential or privileged 
information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL 
PROTECTED] immediately
and destroy all copies of this message and any attachments.

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




RE: Append on top

2004-10-29 Thread Sreedhar . Kalkunte-Venkatachala
open(MYNAME,">>a.txt");


-Original Message-
From: Rajesh Dorairajan [mailto:[EMAIL PROTECTED]
Sent: 29 October 2004 02:12
To: Perl-Beginners ([EMAIL PROTECTED])
Cc: Rajesh Dorairajan
Subject: Append on top


Does anyone know of a way to open a file in append mode and append on top of
the file? 

Thanks in Advance,

--Rajesh 

Visit our website at http://www.ubs.com

This message contains confidential information and is intended only
for the individual named.  If you are not the named addressee you
should not disseminate, distribute or copy this e-mail.  Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses.  The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission.  If
verification is required please request a hard-copy version.  This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]