frostman has asked for the wisdom of the Perl Monks concerning the following question:
Oh wise monks! I am sort of stumped.
I think I see Perl 5.6.1 sorting differently (sometimes) than Perl 5.8.8. (And no, I can't just use 5.8.8.) Specifically, it looks like 5.6.1 has an imperfect notion of keeping "ties" in the order received.
I would greatly appreciate additional eyes on the code below, looking for any stupid mistakes I've made. The really nutty thing is that if I take away the last hashref in @unordered the sucker passes in 5.6.1.
Here's the code:
# show that sorting has changed from 5.6.1 to 5.8.8 use strict; use warnings; use Test::More tests => 1; # I would expect ties to come out in the order in which they are # received. 5.8.8 seems to do that, 5.6.1 does not. my @unordered = ( { pos => 3, val => 0 }, { pos => 5, val => 1 }, { pos => 4, val => 0 }, { pos => 1, val => -1 }, { pos => 2, val => -1 }, { pos => 6, val => 1 }, { pos => 0, val => -2 }, ); my @ordered = sort { $a->{pos} <=> $b->{pos} } @unordered; my @result = sort { $a->{val} <=> $b->{val} } @unordered; is_deeply( \@result, \@ordered ); diag("Your Perl is $]."); diag( "expected val: " . join( ",", map { $_->{val} } @ordered ) ); diag( " result val: " . join( ",", map { $_->{val} } @result ) ); diag( "expected pos: " . join( ",", map { $_->{pos} } @ordered ) ); diag( " result pos: " . join( ",", map { $_->{pos} } @result ) );
Here's the output for v5.8.8 built for darwin-2level:
1..1 ok 1 # Your Perl is 5.008008. # expected val: -2,-1,-1,0,0,1,1 # result val: -2,-1,-1,0,0,1,1 # expected pos: 0,1,2,3,4,5,6 # result pos: 0,1,2,3,4,5,6
And here's the output for v5.6.1 built for i386-linux:
1..1 not ok 1 # Failed test at wtf.pl line 24. # Structures begin differing at: # $got->[5]{pos} = '6' # $expected->[5]{pos} = '5' # Your Perl is 5.006001. # expected val: -2,-1,-1,0,0,1,1 # result val: -2,-1,-1,0,0,1,1 # expected pos: 0,1,2,3,4,5,6 # result pos: 0,1,2,3,4,6,5 # Looks like you failed 1 test of 1.
Of course I'm trying to solve a slightly trickier problem, but this is about as reductive a demonstration as I could come up with. Using a sort subroutine is an option, so if this is a known quirk with a known workaround I'm all ears.
Many thanks in advance for suggestions, reprimands, &c.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is sorting different in Perl 5.6.1 than in Perl 5.8.8?
by Joost (Canon) on Nov 01, 2007 at 00:08 UTC | |
by frostman (Beadle) on Nov 01, 2007 at 01:44 UTC | |
|
Re: Is sorting different in Perl 5.6.1 than in Perl 5.8.8? (stable sort)
by lodin (Hermit) on Nov 01, 2007 at 15:08 UTC | |
|
Re: Is sorting different in Perl 5.6.1 than in Perl 5.8.8?
by salva (Canon) on Nov 02, 2007 at 10:35 UTC |