#use a flag to indicate a match my $found = 0; foreach (@commands_run) { if($_ =~m/clock/) { $found++; last; # we have a match, exit loop } } $found ? print "\n Test -- found pattern -- \n" : print "\n Test -- did not find pattern -- \n"; #### foreach (@commands_run) { if($_ =~m/clock/) { print "found pattern\n"; last; # we have a match, exit loop } }