Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Use of uninitialized value

by Win (Novice)
on Nov 23, 2010 at 10:43 UTC ( #873158=perlquestion: print w/ replies, xml ) Need Help??
Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

if (($numerator > 0) && ($denominator > 0)){
gives me the following error message:
Use of uninitialized value in numeric gt (>) at New_program.pl line 53 +5, <INPUT> line 36.
The values are there because I printed them out. When I try the string 'gt' I get the equivalent message. The variables are initiated using my. And this is set in the correct location. I am not sure what the problem is. Can anyone help please?

Comment on Use of uninitialized value
Select or Download Code
Re: Use of uninitialized value
by moritz (Cardinal) on Nov 23, 2010 at 10:50 UTC
    The values are there because I printed them out.

    How did you print them? What was the result?

    If this is inside a loop, did you print them for all iterations of the loop?

      I printed them to the DOS shell.
      Yes I did print them for all iterations in the loop.
      Note that the print does not take place within the 'if' block. - this is where the error message applies.
Re: Use of uninitialized value
by Anonymous Monk on Nov 23, 2010 at 10:57 UTC
    You've used diagnostics before, why don't you use them now?
Re: Use of uninitialized value
by cjb (Friar) on Nov 23, 2010 at 11:07 UTC

    "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.

      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.
        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.

        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.
        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: Use of uninitialized value
by marto (Bishop) on Nov 23, 2010 at 11:14 UTC
Re: Use of uninitialized value
by Anonymous Monk on Nov 23, 2010 at 11:14 UTC
Re: Use of uninitialized value
by SimonClinch (Chaplain) on Nov 23, 2010 at 11:26 UTC
    Declaration isn't the same as initialisation. For example: my $fred; $fred is declared but has not been given an initial value.

    Update: If you ensure indentation matches scope it might be easier to see the scope of your variables. In this case the declarations are inside the while loop and the line producing the error is not.

    One world, one people

      I have a similar question. I have tried my $filesize = 0; and  my $filesize = -s $$file; and I am getting the same warning only when the file size is 0. I have searched but I have not found anything useful. Use of uninitialized value $filesize in numeric ne (!=) Can you please help?
      $output_file = 'output_file'; backup_the_data(\$output_file); sub backup_the_data { my ($file) = @_; my $filesize = 0; # my $filesize = -s $$file; $filesize = -s $$file; if ($filesize != 0){ copy($$file, $$file . '_backup'); } }
        This worked.
        if ($filesize){ copy($$file, $$file . '_backup'); }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-12-27 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls