Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://830807]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-12-11 19:07 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (310 votes). Check out past polls.