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

use 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;
Output
192.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
Versus the old method of
192.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
- Miller

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.