It sounds like you've already considered and discarded this, but I'll suggest it anyway: IxHash.

But a couple of other options that you didn't mention are:

  1. A normal associative array; that is, two parallel arrays manually kept in sync. It means you'd have to grep or foreach over the list to find your element, but you'd preserve order by pushing entries onto your worklist.

  2. A pseudo-hash. Everyone seems to hate them, and they won't be around in Perl 6, but they seem to do what you want. Basically, you'd do your key lookup in the first element of the array, which is a hash, and then use the returned array index to pull out the appropriate entry. You would likewise push elements onto the array to preserve order, but at the same time add the new entry to the hash.

    my $pseudo_hash = [ { a => 1, b => 2, c => 3 }, 'A', 'B', 'C' ]; print $pseudo_hash->{b}; # prints 'B' print $pseudo_hash->[2]; # prints 'B' print $pseudo_hash->[$pseudo_hash->[0]->{b}]; # prints 'B'

Update: at jdporter's suggestion above, I installed and took a look at IxHash. Interestingly, it's implemented as a synthesis of a pseudo-hash and an associative array. The first element of the array is the hash for key/index mappings, and the two subsequent entries in the array are arrays for the keys and data themselves.


In reply to Re: Hashes made to order by djantzen
in thread Hashes made to order by John M. Dlugosz

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.