Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Use of uninitialized value

by Win (Novice)
on Nov 23, 2010 at 11:23 UTC ( [id://873179]=note: print w/replies, xml ) Need Help??


in reply to Re: Use of uninitialized value
in thread Use of uninitialized value

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"; } } }

Replies are listed 'Best First'.
Re^3: Use of uninitialized value
by Ratazong (Monsignor) on Nov 23, 2010 at 11:31 UTC
    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

        I wouldn't bother wasting your time trying to find an explanation for their comments, they've clearly spent no time debugging the problem or actually reading their code, why would the comments have to make sense?

        if (($numerator > 0) && ($denominator > 0)){ print "..."; # (print) do +es not work here
        The condition will never be true, as at least one of the variables is undef. Therefore the print will never be executed. I assume that this is the reason for the comment does not work here.
Re^3: Use of uninitialized value
by chromatic (Archbishop) on Nov 23, 2010 at 19:19 UTC
    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.

Re^3: Use of uninitialized value
by cjb (Friar) on Nov 23, 2010 at 11:41 UTC
    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.

        You're right, my bad. strike time it is then.

Re^3: Use of uninitialized value
by Anonymous Monk on Nov 23, 2010 at 11:36 UTC
    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://873179]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2025-07-10 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.