http://qs321.pair.com?node_id=1205011


in reply to Re^4: Bolt on where a match is not found to a print script
in thread Bolt on where a match is not found to a print script

Does this fix it?
for my $m (m/$regex/g) { # Update: reworked $matched{$m} = 1; print "$file $_"; }

Full code:

#!/usr/bin/env perl use strict; use warnings; my @files = <c:/perl64/myfiles/*>; # record matching regexes our %matched; my @nums = ('1203', '1204', '1207'); my $regex = '\b(' . join('|', @nums) . ')\b'; # update: fixed + to . for my $file ( @files ) { open my $file_h, '<', $file or die "Can't open $file: $!"; while ( <$file_h> ) { for my $m (m/$regex/g) { # Update: reworked $matched{$m} = 1; print "$file $_"; } } } # Check all nums have been seen for my $num (@nums) { if (not exists($matched{$num})) { print "$num not found\n"; } }

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^6: Bolt on where a match is not found to a print script
by AnomalousMonk (Archbishop) on Dec 06, 2017 at 17:27 UTC

    What Anonymous Monk said:

    while (my ($match) = m/$regex/g) { $matched{$match} = 1; print "$file $_"; }
    gives you an infinite loop.
    c:\@Work\Perl\monks>perl -wMstrict -le "my $regex = qr{ \b f[eio]e \b }xms; ;; $_ = 'xx fee fie foe yy'; while (m/($regex)/g) { my $match = $1; print qq{captured '$match'}; } " captured 'fee' captured 'fie' captured 'foe'
    works better (at least it stops). If you want to get a bit fancy,
    while (m/($regex)/g and my $match = $1) { print qq{captured '$match'}; }
    also works. But the whole "extract multiple matches per line" thing may not even be a real issue! The OPer says nothing about it, and I only raised it as a cautionary point of interest.


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

Re^6: Bolt on where a match is not found to a print script
by QM (Parson) on Dec 07, 2017 at 16:57 UTC
    Fixed original.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re^6: Bolt on where a match is not found to a print script
by Anonymous Monk on Dec 06, 2017 at 16:54 UTC

    this matches, but loops infinitly on the same line print, the script never finishes