$ perl -le ' my @array = ("foo bar baz", "FOO BAR BA?", "a b c", "d e f"); for(1..4) { for my $i (0..$#array) { if( $array[$i] =~ m/(\w{3})/g ) { print "$i: $1"; } else { print "$i: no match"; } } }' 0: foo 1: FOO 2: no match 3: no match 0: bar 1: BAR 2: no match 3: no match 0: baz 1: no match 2: no match 3: no match 0: no match 1: FOO 2: no match 3: no match