my @nums = ('1203', '1204', '1207'); my $regex = '\b(?:' + join('|', @nums) + ')\b';

No way is provided for  if (my ($match) = m/$regex/) { ... } to capture anything (and thus be true). Also, only a single match per line is assumed. (Also  + (addition) is used where  . (concatenation) is intended.)

I would suggest something along the lines of

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @nums = ('1203', '1204', '1207'); my $regex = '\b(?:' . join('|', @nums) . ')\b'; print qq{'$regex'}; ;; my @matches = 'w 1207 x 1203 y 9999 z' =~ m{ $regex }xmsg; dd \@matches; " '\b(?:1203|1204|1207)\b' [1207, 1203]
with the loop being (also untested):
while ( <$file_h> ) { if (my @matches = m/$regex/g) { ++$matched{$_} for @matches; print "$file $_"; } }

Update: Changed code example to include 9999 group.

Update 2: Shorter, IMHO sweeter:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @nums = qw(1203 1204 1207 1111); my ($regex) = map qr{ \b (?: $_) \b }xms, join ' | ', @nums; print $regex; ;; my %seen; for ('w 1207 x 1203 y 9999 z', 'w 11203 x 12033 y 112033 z', 'w 1207 x 1203 y 9999 z 1207 zz', ) { print qq{>$_<} if map ++$seen{$_}, m{ $regex }xmsg; } dd \%seen; ;; my $not_seen = join ' ', grep !$seen{$_}, @nums; print 'num(s) not seen: ', $not_seen || '(none)'; " (?msx-i: \b (?: 1203 | 1204 | 1207 | 1111) \b ) >w 1207 x 1203 y 9999 z< >w 1207 x 1203 y 9999 z 1207 zz< { 1203 => 2, 1207 => 3 } num(s) not seen: 1204 1111


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: Bolt on where a match is not found to a print script by AnomalousMonk
in thread Bolt on where a match is not found to a print script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.