satzbu has asked for the wisdom of the Perl Monks concerning the following question:

dear monks i want to convert an xml file as hash i m using the following code the output not in the correct order how can i preserve the order when convert the xml file as hash

please help me

use XML::Simple; my %xhash=(); my $file= $ARGV[0]; my $simple = XML::Simple->new(); my $xhash = $simple->XMLin($file); print Dumper($xhash);

Replies are listed 'Best First'.
Re: preserve the order of hash
by Old_Gray_Bear (Bishop) on Jun 19, 2010 at 10:55 UTC
    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

Re: preserve the order of hash
by ikegami (Patriarch) on Jun 19, 2010 at 09:27 UTC
Re: preserve the order of hash
by Yary (Pilgrim) on Jun 20, 2010 at 03:26 UTC
    There are modules that will preserve the order in which the hash creates its keys. See this discussion about Tie::IxHash for example. You may need to install it from CPAN.
Re: preserve the order of hash (OT: English compiler)
by toolic (Bishop) on Jun 19, 2010 at 17:27 UTC
    dear monks i want to convert an xml file as hash i m using the following code the output not in the correct order how can i preserve the order when convert the xml file as hash
    $ english your.txt syntax error: expected capital letter 'dear' syntax error: expected comma after 'monks' syntax error: expected period after 'hash' Too many compile errors to continue :)
    Like Perl, the English language has syntax rules. If you can compose Perl code which compiles, as you have done, surely you must have the capacity to construct 3 or 4 English sentences. You'd be surprised how easy it is to occasionally employ capital letters and punctuation marks, with the side benefit of more people understanding your question and providing more help to you.
      May I ask, how many languages you speak? :-)

      Cheers Rolf