I may be misunderstanding completely but it seems to me that if you re-word the question as "grab the next line if I get a match" then a cleaner (IMO) solution comes out. Also you did not say what to do if two consecutive lines match your pattern, should it be ignored or treated in the same way? Below are two solutions which handle each way. (you will need to adapt for your specific data)
my @lines = qw(foo foo2foo bar bar2bar baz2 bell bell1bell); print "Method 1\n"; for my $i (0..$#lines){ if ( $lines[$i] =~ /2/ ){ print "$lines[$i], $lines[$i + 1]\n"; } } print "\n\nMethod 2\n"; for (my $i=0; $i <= $#lines; $i++){ if ( $lines[$i] =~ /2/ ){ print "$lines[$i], $lines[$i + 1]\n"; $i++; } }
--
Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho
In reply to Re: grab only if pattern matches previous line
by greenFox
in thread grab only if pattern matches previous line
by nkpgmartin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |