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.

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;
You can do this in a 1-line from the command-line.
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.


In reply to Re: Delete last record in text file by dragonchild
in thread Delete last record in text file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.