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

Re: sort hash by value

by GrandFather (Saint)
on Mar 25, 2014 at 01:18 UTC ( [id://1079616]=note: print w/replies, xml ) Need Help??


in reply to sort hash by value

You can nest maps in much the same way you can nest for loops. It's not clear that there is a win doing that here, but consider:

my @items = map {$licensehash{$_->[0]}{$_->[1]}} sort {$a->[2] cmp $b- +>[2]} map { my $top = $_; map {[$top, $_, $licensehash{$top}{$_}[3]]} keys %{$licensehash{$_ +}} } keys %licensehash; print "$_->[3]: $_->[0]\n" for @items;

Prints:

Chassis Serial: Creation Date: Creation User: Customer Name: Epoc: Key Type: LMU Version: Lenovo Serial: Lenovo UUID: MAC 1/UUID: MAC 2: NAS: Sync Replication: iSCSI: iTX Version:

Note that @items contains the sorted values rather than the keys which cleans up the print statement somewhat. The map just to the right of the @items assignment does the lookup.

If the code changes take longer than the time saved, it's fast enough already.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1079616]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found