Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • if ( keys %order_by ne @names ) {

    # should probably be

    if ( keys %order_by != @names ) {

    If you didn't mind the performance hit of a tied hash, you could use my Tie::Hash::Sorted

    You could combine your order sub and your sort routine in 1 by using lexicals and references. This way all you would need to do to change the sort order would be to change the array containing the list of keys - the rest would automagically happen. It would also allow you to add/delete/modify keys and values and always have the resul
    • Yup. That's a bug in my code, thanks :)

      I can't use Tie::Hash::Sorted. I'm getting the hash from an external source. Thanks for the idea, though.

      • Well, I am certainly not trying to push Tie::Hash::Sorted as I have never found a use for it myself. I wrote it because my efforts to contact Casey to correct the glaring problems of Tie::SortHash went unanswered (I later learned my emails went to a bit bucket). The original motivation was because it solved a problem a monk was having.

        It does allow you to use an existing hash without damaging it in any way (it makes a copy).
  • Sort::ArbiLex? That's helped me out for this sort of thing innumerable times.
  • Years and years ago I wrote File::Sort (which became a sort(1) for the Perl Power Tools project), and it has some stub function names in the File::Sort namespace that it modifies on the fly. I actually put together a sort subroutine as a string then eval it, and assign the result to *sortsub or whatever.
  • You can also accept a subref, and do something like
    sort { $mysub->($a, $b) }
    That's slower, though my benchmarks didn't make it that much slower (though I may be wrong on that, writing good benchmarks is an art).


  • As far as I understand perlfunc, sort accepts refs to anonymous subs. And I use such code all the time:

    sort $sorter{$field}, @objs;