I do not know the modules you are using well enough to make comments on your code, but if the insertion order is important, then, maybe, a hash is not the best data structure, possibly an array (or maybe an array of hashes) would make more sense.

Sometimes, a good solution is to maintain both an hash and an array in order to have the best of two worlds. The array keeps track of the right order and the hash makes it possible to access quickly to the real data. In this case, your main data structure can be either the hash (possibly HoH, or whatever) and the array (possibly AoH, or whatever), and the other data structure just acts as an auxiliary data structure to get the other property that you need. Writing a subroutine that maintains both structures synchronized is no big deal, perhaps just 3 or 4 code lines if you need insertions, possibly a couple of more if you also need to manage deletions.

All this logics could also be encapsulated in objects, but this might be a bit of an overkill if what you described is all what you need. (OK, yes, I have been discovered, I am somewhat reluctant to OOP for simple problems; to me it is often over-engineering, I tend to prefer the KISS principle. I recently rewrote a 1000+line Java program into a Perl program having less than 75 lines of code. I do not see why I should shoot a bullet in my foot trying to do it the Java way. But this is just my own personal prejudiced opinion, do as you wish.)


In reply to Re: keep hash insert oder, hash inside hash by Laurent_R
in thread keep hash insert oder, hash inside hash by aboboras

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.