in reply to when reading a file how to print a line one above the current line??
No need to chomp it either. Next, if you want to print the line above the current line, you will need to print the line previously read. You could do this by slurping the file into an array, or you could just store the previous line in another scalar:my $file = shift @ARGV;
Notice subtracting one from $. to get the previous line number as well. Everything else looks acceptable, well everything except that typo in your while expression.my $last; while (<MYFILE>) { if(/ALARM:/) { print $. - 1, ": $last"; print "$.: $_"; } $last = $_; }
jeffa
Quiz on Friday ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: when reading a file how to print a line one above the current line??
by graff (Chancellor) on Jun 04, 2002 at 08:04 UTC | |
by Aristotle (Chancellor) on Jun 04, 2002 at 11:50 UTC | |
|
Re^2: when reading a file how to print a line one above the current line??
by Aristotle (Chancellor) on Jun 04, 2002 at 11:55 UTC | |
by dsheroh (Monsignor) on Jun 04, 2002 at 17:10 UTC | |
by Aristotle (Chancellor) on Jun 05, 2002 at 06:06 UTC |