Your skill will accomplishwhat the force of many cannot PerlMonks

### Re^2: Array of hash sorting problem

by murugaperumal (Sexton)
 on Mar 25, 2010 at 10:19 UTC ( #830807=note: print w/ replies, xml ) Need Help??

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";
}

Create A New User
Node Status?
node history
Node Type: note [id://830807]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2016-07-26 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What is your favorite alternate name for a (specific) keyboard key?

Results (236 votes). Check out past polls.