in reply to Module Proposal: Tie::SortHash

KM already mentioned a couple other alternatives.

One I have used is DB_File. Just tie to a BTree and you can get keys back in sorted order. Custom sort functions and all. You can't sort on values, but I almost never would want to be able to do that, and if I did it would almost always be better to do it after the fact.

Reminds me though, a fun gotcha I shocked both the author of DB_File and the authors of Berkeley DB with. Consider the following sort function:

sub { use locale; $a cmp $b; }
If you use this someone can store stuff in a locale sensitive order. Handy for those people in Europe with different alphabets.

What happens if someone changes the locale on their machine? And then starts using the database?

BOOM!
Instantly corrupted database! Completely silent!

So this is a handy sort sub to know about, but use with some caution. :-)

Replies are listed 'Best First'.
RE: RE (tilly) 1: Module Proposal: Tie::SortHash
by cwest (Friar) on Aug 19, 2000 at 00:13 UTC
    Nice gotcha!

    I am aware of that method however, it doesn't seem so straight forward if you ask me... at first I would think... "What the heck do I need a DB_File for?"

    When I think, I want to have my hashes auto sort without doing (hardly) any work at all. I think: search.cpan.org searching for Hash and/or Sort... oh, ther it is (well, would be).

    --
    Casey
    
      Agreed it is not an obvious place to look. OTOH when I learned about tie, it was through DB_File. So from the time I learned that something like this was possible, I knew how to use it to autosort.

      YMMV of course.