To put it as simply as possible -- you can't.

Perl provides two native "Bags of Bits" -- the array (@array) and the hash (%hash). They have different characteristics because they serve different purposes.

An array preserves the order of elements at the expense of search time. To determine if a value exists in an array, you have to examine each element in the array, one element at a time, until either you find your target value or reach the last element. There are tricks you can perform to reduce the number of elements you have to examine (binary search on a sorted array, for example), but on average, you will have to examine more than one element before you determine the presence or absence of a particular value.

A hash optimizes retrieval time at the expense of key order. Determining if a key is (or is not) present in a hash is a single operation -- exists($hash{$value}). No additional elements need be checked. The cost of getting this O(1) search behavior is the loss of control of the key order. Again there are tricks you can do (retrieve the hash-values based on the sorted order of the keys) to impose external order.

The bottom line is that if you want to keep a particular order to the data in the Bag of Bits, you use an array. If you want rapid determination if a particular datum is in your Bag of Bits, you use a hash.

Now, what is the real problem you are trying to solve here -- why do you think you need to have the order of the XML tags preserved after you put them into your data-structure?

----
I Go Back to Sleep, Now.

OGB


In reply to Re: preserve the order of hash by Old_Gray_Bear
in thread preserve the order of hash by satzbu

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.