in reply to How to match previous line

You need something along the lines of
use strict; use warnings; my $previous; while (my $line = <$handle>) { if (defined($previous) && $line =~ /BEEP/ ) { print $previous; } $previous = $line; }

Of course you need additional code to extract the number from $previous, but that doesn't seem to be a problem for you.

Update: as ambrus++ pointed out in the CB this is a good place to use a continue block for assigning to $previous.

Perl 6 projects - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: How to match previous line
by JavaFan (Canon) on Aug 26, 2009 at 16:33 UTC
    Considering that $previous is defined most of the time, it's better to check $line =~ /BEEP/ first. Or just eliminate the check for definedness all together:
    my $previous = ""; while (<$handle>) { print $previous if /BEEP/; $previous = $_; }
    If "BEEP" is on the first line, it just prints nothing. Which is the same effect as not printing (assuming no assignment to $\ has been done).