http://qs321.pair.com?node_id=1032927


in reply to Re: Check if sort has changed order (eq)
in thread Check if sort has changed order

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.

Replies are listed 'Best First'.
Re^3: Check if sort has changed order (eq)
by Krambambuli (Curate) on May 10, 2013 at 10:43 UTC
    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.