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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.