develooper Front page | perl.perl5.porters | Postings from August 2016

Re: [perl #39358] sort with custom subname and prototype ($$)segfaults intermittently

From:
John P. Linderman
Date:
August 10, 2016 21:03
Subject:
Re: [perl #39358] sort with custom subname and prototype ($$)segfaults intermittently
Message ID:
CAC0cEp_nF_4wtONSTfpEVXvmmpm-pueeRJG6yXf=LSuSaQPqBg@mail.gmail.com
#!/usr/bin/perl -w
@scores = (
  '0000.0000.00039:0000.0008.0031.',
  '0000.0000.00032:0000.0008.0024.',
  '0002.0002.00033:0002.0011.0020.',
  '0028.0028.00190:0077.0085.0028.');
@match_indices = (0,1,2,3);
sub sort_by_index($$) {
    my($A,$B) = @_;
    return $scores[$match_indices[$A]] cmp
   $scores[$match_indices[$B]];
}
@match_indices = sort sort_by_index @match_indices;


This could be "fixed" at the expense of allocating *two* extra arrays
instead of just one (which is already more than quicksort, which sorts "in
place"). But I wonder why people think that the contents of any array in
the process of being sorted can participate in any meaningful way in a
comparison. Must all sort implementations guarantee that the intermediate
contents of the array to be sorted are some permutation of the original
array? I don't like the idea of penalizing *all* users for the dubious
intent of *some* users. Segfaults are evil, of course. A better solution
would be to somehow mark an array as "off limits" while a sort is in
progress, but that's way beyond my pay grade.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About