http://www.perlmonks.org?node_id=873158

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Use of uninitialized value
by marto (Cardinal) on Nov 23, 2010 at 11:14 UTC
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?

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Use of uninitialized value
by anonymized user 468275 (Curate) 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'); }
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.
        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.
        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.

Re: Use of uninitialized value
by Anonymous Monk on Nov 23, 2010 at 11:14 UTC