Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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 ( [id://1204965]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Bolt on where a match is not found to a print script
by AnomalousMonk (Archbishop) on Dec 05, 2017 at 16:00 UTC
    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

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

        Fixed original.

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

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

Re^4: Bolt on where a match is not found to a print script
by Anonymous Monk on Dec 05, 2017 at 15:09 UTC

    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

        tried this one, when one of the numbers is matched in a file it keeps printing it over and over on the screen .. so the script never finishes

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found