Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Use of uninitialized value

by cjb (Friar)
on Nov 23, 2010 at 11:07 UTC ( [id://873170]=note: print w/replies, xml ) Need Help??


in reply to Use of uninitialized value

"The variables are initiated using my."

Is the comparison in the same scope as the declaration? I think we need to see more of your code to see where the problem might be.

Replies are listed 'Best First'.
Re^2: Use of uninitialized value
by Win (Novice) on Nov 23, 2010 at 11:23 UTC
    As requested
    More code here:
    foreach (@SQL_queries) { my $Results = $_; my $sth_atl = $dbh->prepare($Results) or die "Couldn't prepare que +ry: ".$dbh->errstr; $sth_atl->execute() or die "Couldn't execute query: ".$sth_atl->er +rstr; my $cols_for_row; while ($cols_for_row = $sth_atl->fetchrow_arrayref) { print OUTFILE "England\t"; print OUTFILE join("\t",@$cols_for_row),"\n"; my $last_element = @$cols_for_row[-1]; my $indicator = @$cols_for_row[-2]; my $numerator; my $denominator; if ($indicator eq "SH"){ $numerator = $last_element; } elsif ($indicator eq "SL"){ $denominator = $last_element; } else { print "\nError: There has been an unexpected string within the sec +ond to last element of the array\n"; } print "\n>>>>>numerator>>>>>>>>>>>> $numerator\n"; # Works here print "\n>>>>>>denominator>>>>>>>>>>> $denominator\n"; # Works her +e if (($numerator > 0) && ($denominator > 0)){ print "\n>>>>>numerator>>>>>>>>>>>> $numerator\n"; # Does not wo +rk here print "\n>>>>>>denominator>>>>>>>>>>> $denominator\n"; # Does not +work here my $expected_ratio_figure = ($numerator)/($denominator); print OUTFILE "\n\nThe expected ratio figure:\t"; print OUTFILE "$expected_ratio_figure\n\n"; } } }
      my $numerator; my $denominator; if ($indicator eq "SH") { $numerator = $last_element; } elsif ($indicator eq "SL"){ $denominator = $last_element; } else { ... } if (($numerator > 0) && ($denominator > 0)){ ...
      The first time you execute the loop, $numerator or $denominator may get initialized, but never both. Therefore you get the error message Use of uninitialized value in numeric gt (>) at New_program.pl line 535.
        I think you're right, but that would not explain his comments works here/ does not work here, the error would be the same at both points
      my $numerator; my $denominator;

      Here both $numerator and $denominator contain undef.

      if ($indicator eq "SH"){ $numerator = $last_element; }

      If that branch is true, $numerator will contain the value of $last_element, which may be undef. $denominator will still contain undef.

      elsif ($indicator eq "SL"){ $denominator = $last_element; }

      If that branch is true (and it will only be true if the first branch is false, in which case $numerator will still contain undef), $denominator will contain the value of $last_element, which may be undef.

      else { print "\nError: There has been an unexpected string within the sec +ond to last element of the array\n"; }

      If this branch is true, both $numerator and $denominator contain undef.

      At this point, at least one of those two variables does contain undef. Both may.

      my $numerator; my $denominator; if ($indicator eq "SH"){ $numerator = $last_element; } elsif ($indicator eq "SL"){ $denominator = $last_element; } else { print "\nError: There has been an unexpected string within + the second to last element of the array\n"; }
      You only ever initialise $numerator or $denominator on the first run through. Something like
      my $numerator = 0; my $denominator = 0;
      might work, if 0 is a suitable default value for you.
        No, it's initialized to undef in every iteration of the loop.
      As requested More code here:

      No, that is not what was requested. There is no way the code you posted could create the error message you get, at the point you say you get it. Its impossible.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-08-04 18:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.