in reply to Sort hash with values
Hash lookups typically involve searching keys for values, so your logic is backwards. Rearranging the hash order and changing values to keys gives something closer to what you probably want.
Update: The mySort routine looks like it's not doing it's job properly or efficiently. If you explain how you want the output sorted, I'm sure someone will have good advice.sub mySort { $a =~ /(\d+)/; my $firstVal = $1; $b =~ /(\d+)/; my $secVal = $1; $firstVal <=> $secVal; } my %IP_Store = ( "UEH1_system_ip" => "11.0.0.1", "UEH11_system_ip" => "11.0.0.11", "UEH25_system_ip" => "11.0.0.3", "UEH111_system_ip" => "11.0.0.25" ); foreach my $key (sort mySort (keys (%IP_Store))) { print "$key\n"; print "System_ip = '$IP_Store{$key}' \n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Sort hash with values
by 2teez (Vicar) on Jun 23, 2013 at 16:20 UTC | |
by Laurent_R (Canon) on Jun 23, 2013 at 21:49 UTC | |
by farang (Chaplain) on Jun 24, 2013 at 02:16 UTC | |
by Anonymous Monk on Jun 24, 2013 at 01:35 UTC |
In Section
Seekers of Perl Wisdom