in reply to Re: Array of hash sorting problem
in thread Array of hash sorting problem

foreach(@aoh) { my %a = %$_; my %c= reverse %a; foreach my $key (sort { $a <=> $b } keys%c) { print "$key=> $c{$key}\n"; } print "\n"; }

Replies are listed 'Best First'.
Re^3: Array of hash sorting problem
by almut (Canon) on Mar 25, 2010 at 10:51 UTC
    foreach(@aoh) { my %a = %$_; my %c= reverse %a;

    The problem with reversing the hash is that if the values aren't unique, e.g.

    { 3 => 15, 4 => 8, 5 => 8, },

    you won't get the desired result...  So why impose unnecessary restrictions for no real gain?  For any reasonably sized hash, creating a reversed copy of it will approximately outweigh the benefits of not having to dereference in the sort function.

    Also, there's no need to create an extra temp hash %a, just write

    my %c = reverse %$_;
Re^3: Array of hash sorting problem
by pavunkumar (Scribe) on Mar 25, 2010 at 10:25 UTC
    Try this things.....
    foreach $hash ( @aoh) { my %new = reverse (%{$hash}); foreach ( sort {$a <=> $b } keys (%new )) { print " $new{$_} :$_ \n "; } print "-------------\n"; }