Greg Grant wrote:

The following script runs on 5.8 but does not run on 5.10.  I
distilled out a short program with the heart of the bug. The output of
this script could be achieved easily without recursion but my real
sort routine does need to be recursive, but I removed most of the
code. Ignore the fact that this doesn't achieve something useful as
is, the point is it runs on 5.8 and not on 5.10.

use strict;

Should also have:

use warnings;


my %SAM;
$SAM{chr2LHet} = 1;
$SAM{chr2RHet} = 1;
$SAM{chr3LHet} = 1;
$SAM{chr3RHet} = 1;

That is usually written as:

my %SAM = (
    chr2LHet => 1,
    chr2RHet => 1,
    chr3LHet => 1,
    chr3RHet => 1,
    );


foreach my $chr (sort cmpChrs keys %SAM) {
     print "CHR=$chr\n";
}
sub cmpChrs () {

Why are you using a prototype? You should only use prototypes if you are trying to imitate one of perl's built-in functions.


     if($a =~ /chr(\d+)/) {

Since you are removing that string anyway, why not just remove it here?


         my $numa = $1;
         if($b =~ /chr(\d+)/) {

ditto.

             my $numb = $1;
             if($numa<  $numb) { return 1; }
             else {
                 $a =~ s/chr\d+//;
                 $b =~ s/chr\d+//;
                 my %temp;
                 $temp{$a}=1;
                 $temp{$b}=1;
                 foreach my $key (sort cmpChrs keys %temp) {
                     if($key eq $a) { return 1; }
                     else { return -1; }

That is usually written as:

                       return $key eq $a ? 1 : -1;


                 }

Why do you need a hash and a foreach loop to compare the TWO values in $a and $b?


             }
         } else { return 1; }
     }
     return 1;
}

I don't see anywhere in that subroutine where you return 0 which indicates that the two values are equal. It appears that the values are only ever less than or greater than each other?


-------------------------------------------------------------------------------------------------------------------------------------------
On 5.8 it does not crash and outputs the following:
CHR=LHet
CHR=RHet
CHR=LHet
CHR=RHet

On 5.10 it crashes with the following message:

Can't undef active subroutine at test.pl line 29.
Attempt to free unreferenced scalar: SV 0x1124fa0, Perl interpreter:
0x10f7010 at test.pl line 29.
Attempt to free unreferenced scalar: SV 0x1124fa0, Perl interpreter:
0x10f7010 at test.pl line 29.

Perhaps you need to declare the subroutine before you define it:

sub cmpChrs;

sub cmpChrs {

    # define sub here
}

-------------------------------------------------------------------------------------------------------------------------------------------

I had to do the following to fix it:

use strict;
my %SAM;
$SAM{chr2LHet} = 1;
$SAM{chr2RHet} = 1;
$SAM{chr3LHet} = 1;
$SAM{chr3RHet} = 1;
foreach my $chr (sort {cmpChrs($a,$b)} keys %SAM) {

1.  Your prototype says that cmpChrs takes zero arguments.

2.  Your use of parentheses suggests that you want to call the
    subroutine and pass its return value to sort but that is not how
    sort works.


     print "CHR=$chr\n";
}
sub cmpChrs () {




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to