#!/usr/bin/perl use warnings; use strict; my @output = qw( Xmatch3X Xmatch4X Xmatch0Xmatch2X nomatch nomatch nomatchnomatch ); for my $x (0, 3) { if ($output[$x+2] !~ /.*match1.*/ and my ($m1) = $output[$x+2] =~ /.*(match2).*/ and my ($m2) = $output[$x] =~ /.*(match3).*/ and my ($m3) = $output[$x+1] =~ /.*(match4).*/ ){ print "Matched: $m1 $m2 $m3.\n"; } }