Hello, babysFirstPerl.
There is another way: using regular expressions. If I am correct this program has to go over input only once and it is't slow? -
use strict; use warnings; my $header_lines = $ARGV[0] // 0; my $footer_lines = $ARGV[1] // 0; my $whole_input; # slurp whole file into one scalar variable {local $/ ; $whole_input = <DATA>}; # (this can exceed memory if data is too much) # define what line is in regular expression language: # not newline x (zero or more times) + one newline after my $line_regex = qr/[^\n]*\n/; # treat whole input as string and substitute lines with empty strings: $whole_input =~ s/\A (?:$line_regex){$header_lines} //x; # delete some lines from the beginning $whole_input =~ s/ (?:$line_regex){$footer_lines} \z//x; # delete some lines from the ending print $whole_input; # now it is not whole, and you can parse __DATA__ Header 1 Header 2 Text 1 Text 2 Text 3 Text 4 Text 5 Footer 1 Footer 2 Footer 3
But if the last line of the file ends not with newline, second regex do not match and don't delete anything.

In reply to Re^2: More efficient way to exclude footers by rsFalse
in thread More efficient way to exclude footers by babysFirstPerl

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.