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

"Use of uninitialized value in print" but why ???

by ultibuzz (Monk)
on Dec 10, 2005 at 19:12 UTC ( #515739=perlquestion: print w/ replies, xml ) Need Help??
ultibuzz has asked for the wisdom of the Perl Monks concerning the following question:

hi all i got the error "Use of uninitialized value in print at line 28" here is the code
#!/usr/bin/perl -w use strict; my $out_file = "out.txt"; my $match_file = "pattern.txt"; my $in_file = "20051208.LOG"; open(my $match_in, '<', $match_file) or die("open failed: $!"); my @matches = <$match_in>; close($match_in); open(my $fh_in, '<', $in_file) or die("open failed: $!"); my @lines = <$fh_in>; close($fh_in); open(OUTFILE, '>>', $out_file) or die("open failed: $!"); foreach my $lines (@lines) { foreach my $matches (@matches) { chomp($matches); my $matchtmp = $matches . ".*SUPSOSODEV" . '|' . $matches . ".*AJT +SOSOCFD" . '|' . $matches . ".*AJTSOSOISI" . '|' . $matches . ".*AJTS +OSOCLA"; if($lines =~ qr/$matchtmp/) { print OUTFILE; } } close(OUTFILE);

Comment on "Use of uninitialized value in print" but why ???
Download Code
Re: "Use of uninitialized value in print" but why ???
by ultibuzz (Monk) on Dec 10, 2005 at 19:19 UTC
    thx goes to robin to tell me i forgot that i dont have anything in $_ ;D Solved ;D
Re: "Use of uninitialized value in print" but why ???
by xdg (Monsignor) on Dec 10, 2005 at 19:19 UTC
    print OUTFILE;

    Maybe because you're not actually printing anything? Are you trying to print lines that match?

    print OUTFILE $lines;

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: "Use of uninitialized value in print" but why ???
by GrandFather (Cardinal) on Dec 10, 2005 at 19:26 UTC

    Note that you could have reduced your sample code to:

    use strict; use warnings; print;

    and still showed the problem. Take a look at I know what I mean. Why don't you? for some suggestions about pruning sample code down to a bare minimum for posting.


    DWIM is Perl's answer to Gödel
Re: "Use of uninitialized value in print" but why ???
by Hue-Bond (Priest) on Dec 10, 2005 at 19:33 UTC

    You better print something, like print OUTFILE $lines;. Moreover, it's not optimal to read into memory the whole contents of files. Give this a try:

    ## Untested open my $match_in, '<', $match_file or die "1st open failed: $!"; open my $fh_in, '<', $in_file or die "2nd open failed: $!"; open my $fh_out, '>>', $out_file or die "3rd open failed: $!"; while (my $lines = <$fh_in>) { while (my $matches = <$match_in>) { chomp $matches; my $matchtmp = qr/$matches.*(?:SUPSOSODEV|AJTSOSO(?:CFD|ISI|CLA))/ +; ## Assuming you want to print $lines print $fh_out $lines if $lines =~ m/$matchtmp/; } } close $fh_out; close $fh_in; close $match_in;

    --
    David Serrano

      I agree to a point, but you want to iterate all of the matches for each line. Wouldn't you need a way to restart the <$match_in> iterator in your example? I think the code would be better written:

      ... my @matches = <$match_in>; ... while (my $lines = <$fh_in>) { for my $matches ( @matches ) { ...

          -Bryan

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-07-25 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (169 votes), past polls