in reply to Sorting a Hash Ref

Sure you can! The sort docs give a similar case and you can always step up to the various sorts discussed in "Data Munging with Perl" if you really want high-octane performance.
my %sorted = sort { $a->{old_file} cmp $b->{old_file} } (keys %{$hashref}); my $sorted = \%sorted; # or put { } around the sort expression to do it one line.
This assumes the month and day fields are both always 2-characters wide. If they are not, then visit CPAN,/a> and find a Date module to do the comparision.

For example Date::Manip has a Date_Cmp function, whose docs say:

This takes two dates and compares them. Almost all dates can be compared using the perl "cmp" command. The only time this will not work is when comparing dates in different timezones. This routine will take that into account.

Replies are listed 'Best First'.
Re^2: Sorting a Hash Ref
by friedo (Prior) on Oct 26, 2005 at 20:31 UTC
    my %sorted = ....

    Everybody repeat after me: YOU CAN NOT SORT A HASH!

    Hashes are unordered. Attempting to sort them just gets you a list which is stuffed back into a hash which has no internal order. You can get a sorted list of keys or values, but you can not sort a hash.

      Everybody repeat after me: YOU CAN NOT SORT A HASH!

      Hm, okay... YOU CAN NOT SORT A HASH!

      Seriously, though, Tie::IxHash.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


      Perl 6 changes:

      • Ordered hashes will probably&hopefully be more accessible, and standard. This isn't certain yet.

Re^2: Sorting a Hash Ref
by bkiahg (Pilgrim) on Oct 26, 2005 at 17:25 UTC
    The dates should always remain 2 characters. But I keep running into an error when I run the script.
    Can't use string ("57e27be8194bb2332a49a942e7aeafeb") as a HASH ref wh +ile "strict refs" in use at ...
    Any ideas on what I'm doing wrong?

      That string, "57e27be8194bb2332a49a942e7aeafeb", is your key (as shown in your original post.) You are trying to use the key itself as a hash ref. You can't do that. You need to look up the hash ref stored in your hash at that key. I.e. you are doing something like $key->{old_file} where you need to be doing something like $yourhash{$key}->{old_file} or possibly $yourhashref->{$key}{old_file}. It's impossible to tell which from your post because you don't show us whether you access your top level hash directly or through a reference.

      -sauoq
      "My two cents aren't worth a dime.";
      
        I guess my understanding of sort leaves much to be desired. Here is the code I am using to build the hash. This below doesn't error but it is not carrying the rest of the data attached with it.
        my %sorted = sort { $patients->{$a}{old_file} cmp $patients->{$b}{old_file} } (keys %{$patients}); my $patients = \%sorted; # or put { } around the sort expression to do it one line.
        or
        sort { $$patients{$a}->{old_file} cmp $$patients{$b}->{old_file} }
        And for some odd reason $patients is no longer a hash ref??
      Well, that is true. What are you not telling us here? That Dumper output doesn't look right. Is that just part of the dump?
        I apologize, yes that is just part of the dump, I cut most of it for space purposes. I thought I still was using the top tier of it though. Here is a copy of the first entry, including the $VAR.
        $VAR1 = { '57e27be8194bb2332a49a942e7aeafeb' => { 'files' => [ { 'file +_type' => 1, 'date +_uploaded' => '2005-09-12', 'file +_name' => 'SOme file PO.tif', 'date +_faxed' => '' }, { 'file +_type' => 2, 'date +_uploaded' => '2005-09-23', 'file +_name' => 'somess 992525 R N.pdf', 'date +_faxed' => '2005-09-23' } ], 'phone' => 'xxx-xxx- +xxxx', 'last_name' => 'Doe +', 'dme_num' => '992525 +', 'old_file' => '2005- +09-12', 'pid' => '57e27be819 +4bb2332a49a942e7aeafeb', 'dme_pid' => '6310b1 +22dad26ec5c6c1c72a15112bb7', 'first_name' => 'Joh +n ' },