Chris Stinemetz wrote:
Any help is appreciated.
It looks like you don't need "regex help".
Once I match HEH how can alter the program to print the contents that
are in the two lines directly above the match?
For example in this case I would like the print results to be:
**01 REPT:CELL 983 CDM 1, CCU 1, CE 5, HEH Timestamp: 10/10/11 00:01:18
#!/usr/bin/perl
use warnings;
use strict;
while(my $hehline =<DATA>) {
chomp $hehline;
if ($hehline =~ /, HEH/) {
print "$hehline \n";
}
}
You could simplify that by not removing the newline and then adding it
back in:
while ( my $hehline = <DATA> ) {
if ( $hehline =~ /, HEH/ ) {
print $hehline;
}
}
And even simpler by not using the $hehline variable:
while ( <DATA> ) {
print if /, HEH/;
}
But, back to your real problem.
I can think of two ways to do it.
Number one: if you are sure that the line you want is _always_ two lines
above you could use an array to hold the line you need:
my @buffer;
while ( my $hehline = <DATA> ) {
push @buffer, $hehline;
shift @buffer if @buffer > 3;
if ( $hehline =~ /, HEH/ ) {
print $buffer[ 0 ];
}
}
Number two: better to just capture the line you require and only print
it when the regular expression matches:
my $capture;
while ( my $hehline = <DATA> ) {
$capture = $hehline if $hehline =~
m{^\d+/\d+/\d+\s+\d+:\d+:\d+\s+#\d+$};
if ( $hehline =~ /, HEH/ ) {
print $capture;
}
}
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/