The solutions others have pointed out are the best you can do if you are reading a Unix-type stream file. (If you are on Unix or Windows, this is the normal type of file.) Some platforms, however, have other types of files (and yes, other types of text files) that are stored differently at the filesystem level in such a way that seeking to a specific line is much less expensive. The index solution is an attempt to fake this, but it's less efficient than the real thing. I don't know how well Perl supports those filetypes, though, and in any case it doesn't help you if you're on Unix or Windows. But if you're developing something that's going to be deployed as a "solution" and you haven't solidified what platform you're going to run it on yet, this is something to consider.

There are also other ways to fake it, besides the index method. If you can pin down the maximum line length, for example, and if you have control over the thing that creates these files, you could use a fixed "record" (line) length, which makes seeking to a specific line very fast (O(1) time). It also makes your file take up more total space, up to twice as much or more, depending on the ratio between typical and maximum line length, but depending on what you're doing it may be a good deal cheaper to get a bigger disk than a faster one, so this could be a good tradeoff.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to Re: Fast way to read from file by jonadab
in thread Fast way to read from file by Hena

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.