file handle problem

N

newbie

Thanks guys, I get the point of not putting large file in memory. I got a
new question. Say I have a bunch of large files, and I want to just get rid
of the last line of each of these large files. Is there a simpler way
without reading in and outputing the file?

Thanks.
 
G

Gunnar Hjalmarsson

newbie said:
Thanks guys, I get the point of not putting large file in memory. I got a
new question. Say I have a bunch of large files, and I want to just get rid
of the last line of each of these large files. Is there a simpler way
without reading in and outputing the file?

Check out Tie::File.
 
J

John W. Krahn

newbie said:
Thanks guys, I get the point of not putting large file in memory. I got a
new question. Say I have a bunch of large files, and I want to just get rid
of the last line of each of these large files. Is there a simpler way
without reading in and outputing the file?

perldoc -f truncate


John
 
J

John W. Krahn

Anno said:
...probably in combination with File::ReadBackwards.

The OP said that he didn't want to read in the file so presumably he already
knows the length of the last line?


John
 
A

Anno Siegel

John W. Krahn said:
The OP said that he didn't want to read in the file so presumably he already
knows the length of the last line?

What are you doing? Applying logic to Usenet postings? :)

Anno
 
U

Uri Guttman

AS> ...probably in combination with File::ReadBackwards.

and you can use the tell() method to get the seek location after reading
in the last line. someone has asked me for a method to get the internal
file handle so he doesn't have to reopen the file. i will be adding that
soonish. then the OP can just do this <untested>:

use File::ReadBackwards ;

my $rb = File::ReadBackwards->new( 'filename' ) ;

$rb->readline() ;
my $tell = $rb->tell() ;
my $fh->handle() ;
$fh->truncate( $tell ) ;

uri
 
A

Andrew Hamm

newbie said:
Thanks guys, I get the point of not putting large file in memory. I
got a new question. Say I have a bunch of large files, and I want to
just get rid of the last line of each of these large files. Is there
a simpler way without reading in and outputing the file?

Did you ever get a solution? Apart from some useful pointers, I can't see
one. There's a few ways. One is a simple re-write loop which stores the
previous line:

my $buffer;
if($buffer = <IFD>) {
while(<IFD>) {
print OFD $buffer;
$buffer = $_;
}
}

the last line will not be printed.

The suggestion to use truncate is also reasonable:

$size = 0;
# $pos gets the (potential) start of the next line
while($pos = ftell IFD, <IFD>) {
# if there really was a next line, save it
$size = $pos;
}

truncate IFD, $size;

NOTE: I haven't actually tested this last one, but it looks reasonable :)
If it fails repost and I'll put in a bit more effort
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top