Your actual question has been answered (sort the keys of the hash), but here is a little more commentary.

You don't actually want to sort two hashes. No really, you said so. You want to compare two hashes. As you say, if a human is comparing them it is easier if they are sorted. But not if the computer is going to do it for you.

Your longer term goal basically wants to take two hashes (%first and %second) as input and list:

You can get the @additions or @deletions by looping through the keys of one hash and doing 'exists' test on the other. The situation is symettric and so you can write that bit as a helper routine and call it twice.

You can get modifications by looping through the keys of either hash (not both) and doing nothing in the loop if the key is in either of the @additions or @deletions arrays. (You might want to invert the arrays into hashes to make an efficient "is this scalar in this array" test.)

But you don't need the keys sorted in any of the loops described above.

Have fun.

PS. If efficency is your goal you can reduce the number of loops in what I have described above. But hey, optimising pseudo-code-written-in-english could be called premature.


In reply to Re: two questions that come together into one grand unified question by jbert
in thread sorting and comparing hashes by ashaman

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.