in reply to grab only if pattern matches previous line

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 } } }

Replies are listed 'Best First'.
Re: Re: grab only if pattern matches previous line
by sauoq (Abbot) on Jul 10, 2003 at 01:15 UTC
    (undef, $blah1) = split /\(/, $lines[$x];

    That syntax is ugly and, more importantly, it doesn't scale. (Say, due to a change in input format, you need the ninth thing on the line instead of the second?) This

    $blah1 = ( split /\(/, $lines[$x] )[1];
    is much cleaner, I think.

    -sauoq
    "My two cents aren't worth a dime.";