in reply to (jeffa) Re: 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??
But why tell him to read a filename at all in the case? I find that if one is taking filenames on the commandline, it's much better to use the diamond operator and let perl decide whether I should be reading from a bunch of files or STDIN, which allows for more flexibility.
And some technical critique - you didn't guard your print $last. This is what I have in mind:____________#!/usr/bin/perl use strict; use warnings; my $prevline; while (<>) { if(/ALARM:/) { print $. - 1, ": $prevline" if defined $prevline; print "$.: $_"; } $prevline = $_; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^2: when reading a file how to print a line one above the current line??
by dsheroh (Monsignor) on Jun 04, 2002 at 17:10 UTC | |
by Aristotle (Chancellor) on Jun 05, 2002 at 06:06 UTC |