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

Help with recurring "uninitialized variable" problem.

by C_T (Scribe)
on Apr 09, 2004 at 16:39 UTC ( [id://343948]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings most wise and helpful monks!

For whatever reason, this problem keeps cropping up, so I'm hoping someone can shed some light on the problem so I can eliminate it once and for all.

This code works:

while (<>) { if (!/^#+/) { ($source, $destination, $bytes) = split; $net_activity{$source}{$destination} += $bytes; }# ignore comments }# read in each line of the net log
This code does not:
my($one_line) = ''; while ($one_line = <>) { if ($one_line =~ !/^#+/) { ($source, $destination, $bytes) = split(/ /, $one_line); $net_activity{$source}{$destination} += $bytes; }# ignore comments }# read in each line of the net log
I get lots of these:
Use of uninitialized value in pattern match (m//) at ./net_traffic2 li +ne 14, <> line 5.
The program still WORKS (i.e. does what it's supposed to do), but it throws up all those warnings first.

I know I'm doing something Very Dumb(TM) here, but if someone could point it out I'd be extremely grateful. I want to understand this whole "unitialized variable" thing because it keeps coming up even when I think I've initialized the variable. Apparently Perl and I have different ideas of what that means, so I want to get in line.

Many thanks!

C_T

Charles Thomas
Madison, WI

Replies are listed 'Best First'.
Re: Help with recurring "uninitialized variable" problem.
by Not_a_Number (Prior) on Apr 09, 2004 at 17:15 UTC

    Change

       if ($one_line =~ !/^#+/)

    to

       if ($one_line !~ /^#+/)

    or

       unless ($one_line =~ /^#+/)

    or if you really want

      if (!($one_line =~ /^#+/))

    (and by the way you can drop the '+' in the regex)

    dave

      if (!($one_line =~ /^#+/))
      That did the trick!

      I THINK I understand why...

      !~ is a new one on me, BTW. Very cool.

      Many thanks!

      CT

      Charles Thomas
      Madison, WI

        B::Deparse can shed some light . . .

        $ perl -MO=Deparse,-p -e '$one_line =~ !/^#+/' ($one_line =~ (not /^#+/));

        So what you were really doing was first matching /^#+/ against $_, taking the logical not of what that returned and then attempting to use the string version of that as a regexp to match against $one_line.

        Update: And to further explain, that meant you were always splitting and hence would sometimes get lines without all the fields you expected and those would be undefined.

Re: Help with recurring "uninitialized variable" problem.
by Ovid (Cardinal) on Apr 09, 2004 at 16:52 UTC

    Try this:

    while (defined (my $one_line = <>)) { if ($one_line =~ !/^#+/) { ($source, $destination, $bytes) = split(/ /, $one_line); $net_activity{$source}{$destination} += $bytes; }# ignore comments }# read in each line of the net log

    Cheers,
    Ovid

    New address of my CGI Course.

      Ovid wrote:
      Try this:
      while (defined (my $one_line = <>))
      This gives the same warnings, I'm afraid.

      C_T

      Charles Thomas
      Madison, WI

        How are you getting your values in @ARGV? I think one of more of them is undef. As a quick test, try adding the following line before the while loop. If it suppresses the warnings, you have undef values there.

        @ARGV = grep defined() => @ARGV;

        Cheers,
        Ovid

        New address of my CGI Course.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://343948]
Approved by TStanley
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-26 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found