Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Bolt on where a match is not found to a print script

by AnomalousMonk (Archbishop)
on Dec 05, 2017 at 14:01 UTC ( [id://1204964]=note: print w/replies, xml ) Need Help??


in reply to Re: 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

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Bolt on where a match is not found to a print script
by QM (Parson) on Dec 05, 2017 at 14:14 UTC
    Ah, yes, I started down one path, then changed tacks.

    I believe if you change this line:

    my $regex = '\b(?:' . join('|', @nums) . ')\b'; # update: fixed concat +enation issue

    to this:

    my $regex = '\b(' . join('|', @nums) . ')\b'; # update: fixed concaten +ation issue

    it does the job.

    For instance, from the debugger (which, note, doesn't play well with my):

    DB<1> $_ = "oh blah dee" DB<2> ($x) = m/(blah)/ DB<3> x $x 0 'blah'

    Update: Fixed concatenation issue

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

      my $regex = '\b(' + join('|', @nums) + ')\b';

      Still the whole addition/concatenation problem.

      Also, moving the capture into  $regex doesn't address multiple captures per line. This may not even be a possibility in the OPer's application (nothing is ever said about this), but I like to defend against things like this.


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

        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

      running the script, looks like its trying to convert the code to numbers

      Argument "1213|1204|1207" isn't numeric in addition (+) at newcheck.pl + line 12. Argument "\\b(" isn't numeric in addition (+) at newcheck.pl line 12. Argument ")\\b" isn't numeric in addition (+) at newcheck.pl line 12.
        ... looks like its trying to convert the code to numbers

        That's because addition is a numeric operation.


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

        Ack, too much Python lately, apologies.
        my $regex = '\b(?:' . join('|', @nums) . ')\b';

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1204964]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-20 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found