in reply to Re: Printing line before matching expression
in thread Printing line before matching expression

Although possibly not terribly efficient, I like the idea of using 2 file handlers, it makes the code quite simple. But I would change it to something like this:

open my $INPUT, "<", $ARGV[0] or die "I couldn't get at input text"; open my $SAME_INPUT, "<", $ARGV[0] or die "I couldn't get at input tex +t"; my $offset = $ARGV[1]; my $line = <$INPUT> for (1..$offset); # discard the n first lines, can +'t do anything with them anyway while (<$INPUT>) { $line = <$SAME_INPUT>; print $line if /$regex/; }

There is also the alternative of slurping the file into an array and walking through the array. Reading the minus $n line is then trivial.

Replies are listed 'Best First'.
Re^3: Printing line before matching expression
by uday_sagar (Scribe) on Sep 11, 2013 at 14:04 UTC
    Yes, concept of offset is good! :-)