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
|