Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Having that cleared, now you can concentrate on your regex issue in line 25 ;-)

I thought about your code and there are some other topics:

  • use lexical filehandles and the three argument form of open; e.g. see: Perl Maven - Open files in the old way
  • avoid "global" variables; e.g. if you need them only as loop variables; use the smallest scope possible; e.g. no my $i; for $i ( ...); better: for my $i ( ... )
  • Shouldn't you print $participant only if the regex match was successful
  • maybe you can use glob() to find the wanted files?

Here's an untested rewrite based upon these ideas:

#!/usr/bin/perl # http://perlmonks.org/?node_id=1120387 use strict; use warnings; my $log_dir = '/home/user1'; my $log_file = "$log_dir/trans.log"; my $trans_dir = '/data/directoy1'; # Log handles if ( not open my $loghandle, ">>", $log_file ) { # lexical hand +le and three argument form of open() # syslog ... # what's this? die "Cannot open file $log_file: $!"; } # maybe instead of opendir ... and readdir ... my @files = grep { -f } glob( "$trans_dir/*OT*" ); foreach my $file (@files) { print STDERR "File: $file\n"; print "$file\n"; open my $fh, "<", $file # same here; l +exical file handle and 3-arg-form of open() or die "Cannot open $file: $!"; while ( my $line = <$fh> ) { if ( (my $participant) = $line =~ m{(.....)/} ) { # expl +icitely use $line for regex print "$participant"; # only + print when match was found } } close $fh; }

In reply to Re^3: Use of uninitialized value in pattern match (m//) by linuxer
in thread Use of uninitialized value in pattern match (m//) by chris01010

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found