my $found = 0; foreach $folder_match(@folders) { if($mk =~ /^\/\Q$folder_match\E$/i) {# if $mk doesnot match to anyone of @folders,then only push $found = 1; } } if (not $found) { push @folder_match ,$mk; }