#INITIAL DATA my @code_array = qw( 7772344 2233421 TMW2222987 TMA2222333 TMW9999988 AMQ8873332 AMQ1111877 ); my @test_suite = qw( 2222987 7772344 TMW2222987 TMD2222333 bbb TMA 111 ); #CODE # this print "Variant 1:\n"; for (@test_suite) { printf "%s is %s\n", $_, /^(?:\w{3})?\Q$_\E$/ ~~ @code_array ? 'match' : 'not match'; } # or that variant print "\nVariant 2:\n"; for my $val (@test_suite) { printf "%s is %s\n", $val, (grep /^(?:\w{3})?\Q$val\E$/, @code_array) ? 'match' : 'not match'; }