On Mon, Nov 22, 2010 at 11:53 AM, Greg Grant <g...@grant.org> 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;
> my %SAM;
> $SAM{chr2LHet} = 1;
> $SAM{chr2RHet} = 1;
> $SAM{chr3LHet} = 1;
> $SAM{chr3RHet} = 1;
> foreach my $chr (sort cmpChrs keys %SAM) {
>    print "CHR=$chr\n";
> }
> sub cmpChrs () {
>    if($a =~ /chr(\d+)/) {
>        my $numa = $1;
>        if($b =~ /chr(\d+)/) {
>            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; }
>                }
>            }
>        } else { return 1; }
>    }
>    return 1;
> }
>
> -------------------------------------------------------------------------------------------------------------------------------------------
>

Declare the subroutine before calling sort. Something like this...

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

The *sort* documentation for 5.12 shows three forms:

   - sort SUBNAME LIST
   - sort BLOCK LIST
   - sort LIST

I'm guessing that the later version of Perl assumed a different form. By
declaring *cmpChrs* beforehand, Perl knows exactly what you want.

-- 
Robert Wohlfarth

Reply via email to