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

Hello, I'm seeing "Use of uninitialized value in numeric eq (==) at line (132 and 136) ." I'm not sure where/what to print in order to debug. I know this error is common, but I haven't found anything helpful while perusing old threads (for example, I've initialized the variables as many other threads have recommended). Feel free to direct me to a helpful, existing thread!

#initiate S for the whole genome #chrboundst = chr start index #chrbound = chr end index my $k=0; my @S=undef; #open OUT, "> temp.0"; my %chrboundst; my %index; # correct? my @fix; my @score; my %chrbound; foreach my $chr (keys %chrsize) { $chrboundst{$chr}=$k; for (my $i=0; $i<=$chrsize{$chr}; $i++) { $index{$k}="$chr\t$i"; $fix[$k]=1 if ($breakpoint{$chr}{$i}==1); # line 132 $score[$k]=$piRNA{$chr}{$i}-$penalty; $S[$k]=$S[$k-1]+$score[$k] if ($i>0); $S[$k]=$score[$k] if ($i==0); $S[$k]=0 if ($S[$k]<0 || $fix[$k]==1); # line 136 $k++; } $chrbound{$chr}=$k-1; # mark the chr end for recalculation }

Replies are listed 'Best First'.
Re: Use of Uninitialized Value- Multiple Errors
by holli (Abbot) on May 28, 2019 at 17:36 UTC
    I'm not sure where/what to print in order to debug
    Well, the error message tells you quite clearly. $breakpoint{$chr}{$i} is undefined. That of course never sets $fix[$k] and causes the second warning later on. I think you want something like this:
    $fix[$k] = 0; $fix[$k] = 1 if (defined $breakpoint{$chr}{$i} && $breakpoint{$chr}{$i +}==1);
    Or shorter
    $fix[$k] = defined $breakpoint{$chr}{$i} && $breakpoint{$chr}{$i}==1 ? + 1 : 0;


    holli

    You can lead your users to water, but alas, you cannot drown them.

      Thanks! How did you know that  $breakpoint{$chr}{$i} was undefined instead of  $fix[$k] ?

        In your code they were both undefined and I understood this by reading the code and understanding the error message. Which says clearly there is an undefined value used in a numeric comparison aka ==. There is only one thing that gets compared numerically in that line
        $fix[$k]=1 if ($breakpoint{$chr}{$i}==1);
        Now if that condition is false then $fix[$k] will also be undefined because it is never set to something else elsewhere. But later on you use it in another numeric comparison
        $S[$k]=0 if ($S[$k]<0 || $fix[$k]==1);
        And that triggers the second warning. My fix works so that I check for definedness of the value in the hash and making sure all values in the @fix are defined (either 0 or 1 instead of undefined and 1).


        holli

        You can lead your users to water, but alas, you cannot drown them.

        If @fix is only used in that loop consider removing it.

        for my $i (0 .. $chrsize{$chr}) { $index{$k} = "$chr\t$i"; $score[$k] = $piRNA{$chr}{$i} - $penalty; if ( defined $breakpoint{$chr}{$i} && $breakpoint{$chr}{$i}==1 ){ $S[$k] = 0; } else { if ($i == 0) { $S[$k] = $score[$k]; } else { $S[$k] = $S[$k-1]+$score[$k]; } $S[$k] = 0 if $S[$k]<0; } $k++; }
        poj
        $fix is undefined because line 132 fails due to undefined element in %breakpoint.
        Bill
Re: Use of Uninitialized Value- Multiple Errors
by NetWallah (Canon) on May 28, 2019 at 17:36 UTC
    The most likely case is that
    $breakpoint{$chr}{$i}
    is undefined at the time you are getting those messages.

    The way I would debug that is to use the perl debugger:
    perl -d <your-script>
    and add a "conditional breakpoint" at that line:

    b 132 !defined($breakpoint{$chr}{$i})
    once the debugger is stopped , you can examine("x") the values in $chr, $i etc, and analyze why they are not what you expect.

                    "It's ten o'clock... Do you know where your AI programs are?"

Re: Use of Uninitialized Value- Multiple Errors
by BillKSmith (Monsignor) on May 28, 2019 at 18:00 UTC
    The errors you report tell you that the hash %breakpoint is not initialized properly. Because of this, @fix does not get initialized. We cannot offer any more help without more information. Try dumping the array %breakpoint right before your first foreach loop. If it does not contain exactly what you expect, you must fix that first. If the problem persists, post enough of your code that we can run it and reproduce the problem.
    Bill
Re: Use of Uninitialized Value- Multiple Errors
by Marshall (Canon) on May 28, 2019 at 23:27 UTC
    The first thing that you should do is add : use strict; use warnings to the code.
    There are many errors past the runtime errors that you described. "use strict;" is important because it is a compile time "sanity check".

    for (my $i=0; $i<=$chrsize{$chr}; $i++) { looks suspicious and may be an "off by one" error.
    I couldn't figure out what this code is actually supposed to to in terms of an algorithm - just not enough context for me to do that.

    use strict; use warnings; #initiate S for the whole genome #chrboundst = chr start index #chrbound = chr end index my $k=0; my @S=undef; #open OUT, "> temp.0"; my %chrboundst; my %index; # correct? my @fix; my @score; my %chrbound; foreach my $chr (keys %chrsize) { $chrboundst{$chr}=$k; for (my $i=0; $i<=$chrsize{$chr}; $i++) { $index{$k}="$chr\t$i"; $fix[$k]=1 if ($breakpoint{$chr}{$i}==1); # line 132 $score[$k]=$piRNA{$chr}{$i}-$penalty; $S[$k]=$S[$k-1]+$score[$k] if ($i>0); $S[$k]=$score[$k] if ($i==0); $S[$k]=0 if ($S[$k]<0 || $fix[$k]==1); # line 136 $k++; } $chrbound{$chr}=$k-1; # mark the chr end for recalculation } __END__ Global symbol "%chrsize" requires explicit package name (did you forge +t to declare "my %chrsize"?) at line 16. Global symbol "%chrsize" requires explicit package name (did you forge +t to declare "my %chrsize"?) at line 18. Global symbol "%breakpoint" requires explicit package name (did you fo +rget to declare "my %breakpoint"?) at line 20. Global symbol "%piRNA" requires explicit package name (did you forget +to declare "my %piRNA"?) at line 21. Global symbol "$penalty" requires explicit package name (did you forge +t to declare "my $penalty"?) at line 21. Execution of aborted due to compilation errors.