Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by QM (Parson)
on Dec 05, 2017 at 11:44 UTC ( #1204947=note: print w/replies, xml ) Need Help??


in reply to Bolt on where a match is not found to a print script

Something like this (untested):
#!/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> ) { if (my ($match) = m/$regex/) { $matched{$match} = 1; print "$file $_"; } } } # Check all nums have been seen for my $num (@nums) { if (not exists($matched{$num})) { print "$num not found\n"; } }

I would probably remove the @files and pass them on the command line (changing the for loop to while (<>)).

You might also do something more generic around the numbers, and allow those to be passed on the command line as well -- but this will take some command line parsing, or one of the GetOpt modules.

Update: Fixed capture problem, as noted by AnomalousMonk.

Update 2: Fixed concatenation issue.

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

Replies are listed 'Best First'.
Re^2: Bolt on where a match is not found to a print script
by AnomalousMonk (Bishop) on Dec 05, 2017 at 14:01 UTC
    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:  <%-{-{-{-<

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

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-04-11 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?