Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Uninitialized Value

by amearse (Sexton)
on Dec 14, 2001 at 23:50 UTC ( [id://132049]=perlquestion: print w/replies, xml ) Need Help??

amearse has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, In the script below I am trying to parse through emails to find only certain elements. The code works, however, I am confounded by these warnings.
C:\grappa>grappa1-0.pl Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49. Use of uninitialized value in print at C:\grappa\grappa1-0.pl line 49.
There are ten warnings, and likewise, there are ten messages that I'm sorting through. Here is the output. If you know the problem, can you advise? Rock 'n..
Saturday, December 01, 2001 06:03 PM Linda Lutnam http://dt.com/link/click?lid=310058 <46434979-220011153023294428@eiiance.com> Times Ten.
#!/usr/bin/perl/ -w ### Grappa ### use warnings; use strict; #use DBI(); my $flag = 0; my $list = shift || 'c:\grappa\lists\list.txt'; my $output0 = shift || 'c:\grappa\output\output0.txt'; my $date = 'Date: '; my $id = 'Message-Id: '; my $ID = 'Message-ID: '; my $subj = 'Spamvertised website: '; my $from = 'From: '; my @data = ""; open (LIST, "$list") || die "Can't Open $list"; my @list=<LIST>; close(LIST); foreach (@list){ if(s/.*$date//){ $data[0] = $_; $flag =0; } if(s/.*$from//){ $data[1] = $_; $flag =0; } if(s/.*$subj//){ $data[2] = $_; $flag =0; } if(s/.*$id//){ $data[3] = $_; $flag =0; } if(s/.*$ID//){ $data[4] = $_; $flag =0; open STDOUT,">>$output0"; print STDOUT @data; # This is line 49 close STDOUT; } }

Replies are listed 'Best First'.
Re: Uninitialized Value
by mortis (Pilgrim) on Dec 15, 2001 at 00:00 UTC
    @data starts out with 1 element: "" (the empty string), from when you initialize it. $data[1], $data[2], $data[3], and $data[4] are all uninitialized.

    As you go through the last foreach loop, you (might) assign $_ to specified elements of @data. On line 49, you print all the elements of @data - some of which are obviously uninitialized.

    You can initalize them with something like the following:

    my @data = ('','','','','');
    To have it start it's life with 5 empty strings.

    Also, consider moving the open/close statemens outside of the foreach loop.

    open STDOUT, ">>", "$output0" or die "$output0: $!\n"; foreach (@list) { ... print STDOUT, @data; } close STDOUT;
    Since you're appending to the output file, you might as well just open it once and be done with it.

    Update: Fastolfe pointed out that the initialization should have been with the empty string instead of undef...thanks.

      You don't want to "initialize" to undef. All scalars start out as 'undef', so this is precisely what perl is looking for when emitting this warning. If you want to initialize a variable, initialize it to something that makes sense for your application. In this case, @data is being printed out as a string, so an empty string might be appropriate.
      $ perl -we 'print undef' Use of uninitialized value in print at -e line 1.
Re: Uninitialized Value
by petral (Curate) on Dec 15, 2001 at 00:39 UTC
    $ perl -lwe'$x[2]="x";print @x' Use of uninitialized value at -e line 1. Use of uninitialized value at -e line 1. x $
    Like kwoff and mortis said, one of your patterns isn't matching before /$ID/, so there's a hole in your data.  Notice, that if one message matched it's value would remain for all those after.  You should probably put the    @data = ('','','','','');   inside the loop.

      p
Re: Uninitialized Value
by kwoff (Friar) on Dec 14, 2001 at 23:58 UTC
    It means a variable wasn't initialized. (`perldoc perldiag`, search for that warning)
    In the message you gave, I don't see the things you're trying to match ('Date: ', 'Message-Id: ', etc.), so I'm guessing that would be why.

Log In?
Username:
Password:

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

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

    No recent polls found