Elie De Brauwer wrote:
You could for example do that by buffering them. Following example reads from standard input. No that you should replace the regexp by something with more meaning in your context. This application also assumes that there will be at least three lines of input.

my $a=<STDIN>;
chomp $a;
my $b=<STDIN>;
chomp $b;
my $c=<STDIN>;
chomp $c;
while($c !~ /-1$/){
    $a=$b;
    $b=$c;
    $c=<STDIN>;
    chomp $c;
}
if($c =~ /-1$/){ print "$a\n$b\n$c\n";
}else{
    print "Not found\n";
}

hth
E.

You could keep a sliding list of lines:

#!/usr/bin/perl

use strict;
use warnings;

my $kept_lines = 3;
my @lines = ();

while( <> ){
  push @lines, $_;
  shift @lines if @lines > $kept_lines;
  chomp;
  if( /:-1$/ ){
    print @lines;
  }
}

__END__



--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to