in reply to More efficient way to exclude footers

This is probably overkill, but my first thought was, can't you do a system call to the Unix command wc (which can give you the number of lines in your input file?)

And CPAN also has two modules that I think will give you the same functionality.

Otherwise, for the headers, I'm thinking you could just add a variable which keeps track of how many lines you've read in. Then you'd be able to calculate whether you're still dealing with header rows.
while(<INPUT>) { $linecount++ ; next if $linecount <= $numHeaders;
The footer problem is a bit trickier, but after the header you could first just push every row into an array as a separate string element. Then you'd know exactly how many lines you're dealing with, and you can parse only the elements that lead up to @array[ scalar @array - $numFooters ]

(Updated to remove extraneous parentheses and use scalar on @array)

Replies are listed 'Best First'.
Re^2: More efficient way to exclude footers
by AnomalousMonk (Archbishop) on Aug 19, 2015 at 18:18 UTC
    @array[ length(@array) - $numFooters) ]

    But  length(@array) returns the length of the string representing the number of elements in the array:

    c:\@Work\Perl\monks\babysFirstPerl>perl -wMstrict -le "my @array = (0 .. 10_000); print length(@array); " 5
    So what you really want is  @array[ @array - $numFooters ] (untested). (Also: There's an extra right-paren after  $numFooters in the original reply.)

    ... for the headers, I'm thinking you could just add a variable which keeps track of how many lines you've read in. ... after the header you could first just push every row into an array ... parse only the elements that lead up to @array[ length(@array) - $numFooters) ]

    With necessary semantic corrections, isn't this pretty much exactly what Athanasius suggested above?


    Give a man a fish:  <%-{-{-{-<

Re^2: More efficient way to exclude footers
by Laurent_R (Canon) on Aug 19, 2015 at 18:59 UTC
    while(<INPUT>) { $linecount++ ; next if $linecount <= $numHeaders; # ... }
    Why not, but if the file is very large you're doing a $linecount incrementation and a test for every line in the file.
    <INPUT> for 1..$numHeaders;
    is just reading the first $numHeaders lines of the file and throws them away, and you don't add any overhead for the rest of your file. And, BTW, the $. special variable contains the line count of the last read file handle, so that you don't need the $linecount incrementation.