At the risk of stating the obvious, for your 'lines 1034-1047' problem I can see two efficient solutions.

1: If you have control of the source file make it fixed-width (possibly not even newline terminated records), then seeking a particular 'line' (record) is a simple bit of math and a seek().

2: If the file must have variable length lines, and assuming you do the mid-file extraction frequently and that the file persists for a relatively long time...
Scan the file first for newlines.
Record the position of each newline in a second index file.

To look at a particular line:
Open up the index file.
Scan to the line# you're looking for.
seek() on the file you want to look at.

If the requested lines are past the end of the index:
seek() to the last known newline in the source file
index to the end (appending to your index file).

Another optimization is to store the index in binary format (ie: integers) so that you can find the index for line #27231 simply by seeking to (size_of_rec * 27231).

The algorithm in 2 can be implemented in Perl, but would be simple to do in C or C++ which would make it run pretty fast too.

Steve-


In reply to Re: part two by smoo
in thread Simulating UNIX's "tail" in core Perl by gryphon

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.