[Aside: It seems that not using newlines inside <code> tags prevents the [download] link from appearing. In the future, some of your posts may benefit from the [download] link.]

Ordinarily, sort would be used:

my @sorted_scores = sort keys %hashFinal; my @sorted_line_nums = map { $Reversevalues{$_} } @sorted_scores;
Your question seems odd to me, because if this is representative code, then @sorted_scores and @sorted_line_nums will have corresponding values. There are no situations I've come across that need the internal hash "order" to be preserved. Since the internal hash order is data dependent, version dependent, (and possibly platform dependent,) I've always used sort when I need order.

I can imagine some situations where you want the insert order, yet still need to use the associative lookup capability of a hash. In that case, gaal's suggestion of Tie::IxHash seems right on. Any other order implies an ability to discriminate a correctly ordered list and an incorrectly ordered list, and again sort is recommended.

Perhaps you've just overlooked that sort can take a user supplied sort subroutine?

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: Keeping "keys" in order when passed to an Array by QM
in thread Keeping "keys" in order when passed to an Array by Gavin

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.