in reply to Check if sort has changed order

my @f = ( 4, 2 ); my @g = sort @f; if( "@f" eq "@g" ){ die "already sorted, can't have that\n" }

Replies are listed 'Best First'.
Re^2: Check if sort has changed order (eq)
by Anonymous Monk on May 10, 2013 at 09:47 UTC

    Ha, stupid memory allocation tricks

    my @f = ( 4, 2 ); print join ' ', int \$_, \$_, "$_\n" for @f;print "\n"; @f = sort @f; print join ' ', '#', int \$_, \$_, "$_\n" for @f;print "\n"; my $prev = 0; for my $next ( @f ){ if( $prev ){ die "BEEN SORTED" if $prev > \$next; } $prev = \$next; } __END__ $ perl fa 4165804 SCALAR(0x3f90ac) 4 4165980 SCALAR(0x3f915c) 2 # 4165980 SCALAR(0x3f915c) 2 # 4165804 SCALAR(0x3f90ac) 4 BEEN SORTED at fa line 8.
      That won't work for whatever sort condition.

      I'd probably rather compare the MD5 checksum of the scalar references in the list before and after the sort.

        That won't work for whatever sort condition.

        Sure it will, its comparing memory addresses not data, so the sort condition is irrelevant; only relevant are how much ram you have and how you initialized the array; if the memory addresses aren't sequential before sorting, it won't work.

Re^2: Check if sort has changed order (eq)
by BillKSmith (Monsignor) on May 10, 2013 at 12:24 UTC
    This seems to be a very clever way "to actually compare the lists."
    Bill