Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Use of uninitialized value in pattern match (m//)

by linuxer (Curate)
on Mar 17, 2015 at 23:22 UTC ( #1120398=note: print w/replies, xml ) Need Help??


in reply to Re^2: Use of uninitialized value in pattern match (m//)
in thread Use of uninitialized value in pattern match (m//)

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; }

Replies are listed 'Best First'.
Re^4: Use of uninitialized value in pattern match (m//)
by chris01010 (Novice) on Mar 18, 2015 at 12:43 UTC

    Thanks Linuxer, you adaption worked a treat (inluding glob)

    Just so people know I have only been on a 3 day course and I am "self teaching" the rest. So I really do appreciate the assistance and suggestions

    That being said :0), I am now moving the script on so that it reads each line and tries to match 2 values, both of which can be either one value or another

    I.E the first value can be 7722 or 59001 (appearing before the first occurence of /) and the second value can be N or U or C (which is the 362 character on the line)

    Once it matches both it should print the line, if not skip past it.

    I've had a go, but once again getting an error

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1120387 use strict; use warnings; my $log_dir = '/home/smithc'; my $log_file = "$log_dir/trans.log"; my $trans_dir = '/data/FSA_Retrieval'; # 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 (/(7722/|59001/)/ && /.{361}(N|U|C)/) { print; } } close $fh; }

    When I run a perl -c against the script I get the compile error

    Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE 7722/ at test2.pl line 37

      /(7722/|59001/)/

      If the slash is used as the delimiter, you can't use it as literal, you have to backslash it. Or, switch to a different delimiter:

      /(7722\/|59001\/)/ # or better: m=(7722/|59001/)=
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-05-30 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?