As johngg and all others pointed out, %hashes are inherently unsorted so you simply must save an array of keys sorted however you like.
However, your IPs might not be sorted how you want. By using string compare cmp, you're going to end up with the ip 1.0.0.100 coming before 1.0.0.2. Is that what you want? If it isn't, then you simply need to do a Schwartzian Transform
Outputuse strict; my %ipaddr = ( a => '192.168.0.1', b => '192.168.0.3', c => '192.168.0.200', d => '192.168.2.1', e => '192.168.100.1', f => '192.168.1.1', ); # The old method: # my @sortedKeys = sort {$ipaddr{$a} cmp $ipaddr{$b}} keys %ipaddr; my @sortedKeys = map {$_->[0]} sort {$a->[1] cmp $b->[1]} map {[$_, join '.', map {sprintf "%03d", $_} split '\.', $ipaddr{$ +_}]} keys %ipaddr; print "$ipaddr{$_}\t$_\n" for @sortedKeys;
Versus the old method of192.168.0.1 a 192.168.0.3 b 192.168.0.200 c 192.168.1.1 f 192.168.2.1 d 192.168.100.1 e
- Miller192.168.0.1 a 192.168.0.200 c 192.168.0.3 b 192.168.1.1 f 192.168.100.1 e 192.168.2.1 d
In reply to Re: Save the resultant of " hash sorted by values "
by wind
in thread Save the resultant of " hash sorted by values "
by sundeep
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |