I'll second grep's suggestion that you look into database support and other already-invented wheels. That begs the question you asked, though, of how to be efficient about reading files, and what use could be made of $_ and $. in doing so.

As it happens, you posted this right after some cb discussion of readability, maintainability, C-style operators, and precedence. My suggestion here is an example of code I consider readable and all those other good things, but which depends crucially on "esoteric" details like operator precedence. I believe it to be highly optimized in the good way, by doing as little as possible inside the loop.

It sounds like you have most of the pieces in mind. Assuming WEBLOG is opened successfully, $sep is the record seperator, and $offset and $span are as you describe:

{ local $/ = $sep; my $done = $offset + $span ; while (<WEBLOG>) { chomp, print if $. == $offset .. $. == $done && last; } }
That uses the "flipflop" operator, .. in scalar context. It returns false until its first argument is true. Then it ignores its first argument and returns true until after its second argument becomes true. I use that so only one comparison is done for each record read. last is called to stop reading at that point, with && used because it has higher precedence than .. so that last is short-circuited out until it is needed.

chomp and print are done in sequence (comma operator) only if the flipflop is returning true.

After Compline,
Zaxo


In reply to Re: Anti-slurp weblog by Zaxo
in thread Anti-slurp weblog by newrisedesigns

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.