in reply to More efficient way to exclude footers
Hello babysFirstPerl, and welcome to the Monastery!
Am I correct in thinking that the file begins with a single header of $ARGV[0] lines and ends with a single footer of $ARGV[1] lines? If so, the following approach should do what you want. It reads the file exactly once, and processes it on-the-fly so that the number of lines held in memory never exceeds one plus the number of lines in the footer:
#! perl use strict; use warnings; my $header_lines = $ARGV[0] // 0; my $footer_lines = $ARGV[1] // 0; <DATA> for 1 .. $header_lines; # Throw away the header my @lines; while (<DATA>) { push @lines, parse_line($_); print shift @lines if @lines > $footer_lines; } sub parse_line { my ($line) = @_; # ...Parse $line... return $line; } __DATA__ Header 1 Header 2 Text 1 Text 2 Text 3 Text 4 Text 5 Footer 1 Footer 2 Footer 3
Output:
0:50 >perl 1348_SoPW.pl 2 3 Text 1 Text 2 Text 3 Text 4 Text 5 0:55 >
Update: A couple of additional points:
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: More efficient way to exclude footers
by AnomalousMonk (Archbishop) on Aug 19, 2015 at 17:25 UTC | |
|
Re^2: More efficient way to exclude footers
by rsFalse (Chaplain) on Aug 19, 2015 at 17:31 UTC | |
by AnomalousMonk (Archbishop) on Aug 19, 2015 at 17:51 UTC | |
by rsFalse (Chaplain) on Aug 19, 2015 at 18:09 UTC |