While I'll give a ++ to Tomte because it does what you want based on your code. The $matched flag thing is rather cludgy. It may make your code harder to read down the road. There are some better options which may be easier to decipher if you change your for loop a little. For instance:
@lines=( "request(goodstring", "START_TIME,some goodness", "request(BADSTRING", "START_TIME,some bad stuff", "request(randomstring", "START_TIME,more goodness", ); for my $x (0..$#lines) { if($lines[$x]=~/request/) { (undef, $blah1) = split /\(/, $lines[$x]; if($blah1!~/BADSTRING/) { (undef, $blah2)=split /,/, $lines[$x+1];#<---Plus 1 gets next l +ine print "$blah1:$blah2\n"; } } } ################# ## Output goodstring:some goodness randomstring:more goodness
Since I use a counter to access elements in @lines, I can easily reference the next line of the input without keeping track of a flag. If you want to skip checking the line with $blah2 in it for /request/ you have to use the three argument version of for and then increment the counter after you print:
for ($x=0;$x<=$#lines;$x++) # Three arguments are treated differently than using # the for loop with the list argument { if($lines[$x]=~/request/) { (undef, $blah1) = split /\(/, $lines[$x]; if($blah1!~/BADSTRING/) { (undef, $blah2)=split /,/, $lines[$x+1];, print "$blah1:$blah2\n"; $x++; #<--------Increment to skip the next line } } }
In reply to Re: grab only if pattern matches previous line
by pzbagel
in thread grab only if pattern matches previous line
by nkpgmartin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |