Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Sort hash with values

by 2teez (Vicar)
on Jun 23, 2013 at 15:59 UTC ( [id://1040330]=note: print w/replies, xml ) Need Help??


in reply to Sort hash with values

Hi Rahul Gupta,

Really, I don't know how you wanted your hash sorted but I guess you would like to sort using "keys" since, your values from your OP are "kind" of sorted, following the numbers on the names like UEH1_system_ip, UEH11_system_ip,.. etc.
The following shows one way of doing it:

use warnings; use strict; my %ip_store = ( "11.0.0.1" => "UEH1_system_ip", "11.0.0.11" => "UEH11_system_ip", "11.0.0.3" => "UEH25_system_ip", "11.0.0.25" => "UEH111_system_ip" ); my @ip_data = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { /\.([0-9]+)$/; [ $_, $1 ] } keys %ip_store; print $ip_store{$_}, $/, $_, $/ for @ip_data;
...produces ...
UEH1_system_ip 11.0.0.1 UEH25_system_ip 11.0.0.3 UEH11_system_ip 11.0.0.11 UEH111_system_ip 11.0.0.25

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-28 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found