Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what 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"; }


Comment on Re^2: Array of hash sorting problem
Download Code
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"; }
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 %$_;

Log In?
Username:
Password:

What's my password?
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 pondering the Monastery: (11)
As of 2014-07-28 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (193 votes), past polls