in reply to Delete last record in text file
my $stuff = 'file.txt'; open(F, "$stuff") || die "Can not open file: $!\n"; my @records = (<F>); print "@records\n"; s/lastlineofdata//; #This is the part I am struggling with close(F);
Some notes:
Sounds like you want to delete the last line of the array.
Now, just because you do that, you aren't actually affecting the file. You would have to write back to the file what the file should now look like.
You can do this in a 1-line from the command-line.use strict; use warnings; use IO::File; my $filename = 'file.txt'; my $in_fh = IO::File->new($filename) || die "Can not open '$filename' for reading: $!\n"; my @records = <$in_fh>; $in_fh->close; print "@records\n"; pop @records; my $out_fh = IO::File->new(">$filename") || die "Cannot open '$filename' for writing: $!\n"; print $out_fh, @records; $out_fh->close;
perl -pi -e 'last if eof()' file.txt
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|