A different approach would be to use seek to position the file pointer some number of bytes from the end of your file, 10,000 say, and then use read to read those last 10,000 bytes of the file into buffer held in a scalar variable. You could then open another filehandle on a reference to that scalar which would allow you to read the last 100-200 lines of your file (depending on line lengths) into an array without having to read the whole file. Something like (not tested):

open my $bigFH, q{<}, q{myBigFile} or die $!; seek $bigFH, -10000, 2; read $bigFH, my $last10k, 10000; open my $last10kFH, q{<}, \ $last10k or die $!; my @lastLines = <$last10kFH>;

Be aware the first line you read will most likely be a partial line and this approach will not help if you need to know specific line numbers; for that the while loop would be required.

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: Large text files into arrays, accessing final elements by johngg
in thread Large text files into arrays, accessing final elements by isabella423

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.