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.


In reply to Is sorting different in Perl 5.6.1 than in Perl 5.8.8? by frostman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.