Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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);

Replies are listed 'Best First'.
Re: "Use of uninitialized value in print" but why ???
by GrandFather (Sage) 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 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 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

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

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?
[Corion]: Hi marto!
[Corion]: I hope you had a good weekend!
[Discipulus]: good morning again monks!
[choroba]: Good morning!
[Corion]: I have a rough plan to release an ImageNet classifier in Perl (well, using pretrained data, but still a nice toy)
Discipulus is going mad with the new win10 client.. tried mv Cortana /dev/null but no succes
[Corion]: Ouch - another pretrained set is available, using images from IMDB and Wikipedia. So I expect interesting results for non-PR images where people are pictured that are not made up

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-09-25 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (277 votes). Check out past polls.

    Notices?