in reply to Re^2: when reading a file how to print a line one above the current line??
in thread when reading a file how to print a line one above the current line??

Good point, but, personally, I'd do it as
my $prevline = ''; ... print $. - 1, ": $prevline";
This way doesn't have the overhead of testing every time whether $prevline is defined. Or you could initialize it to "BOF\n" for a more explicit indication of what's going on.

Replies are listed 'Best First'.
Re^4: when reading a file how to print a line one above the current line??
by Aristotle (Chancellor) on Jun 05, 2002 at 06:06 UTC
    I thought about the efficiency issue, but I guess it boils down to personal choice. With the guarded print you don't get to see a -1:\n should the pattern happen to match on the first line, and really that single test does very little to performance. We could pull out Benchmark here but I suspect the difference would be within measurement jitter (how is this exactly called in English?) for any less than a few hundred thousand lines.

    Makeshifts last the longest.