in reply to sort order of imported xml data?

Xenofur:

XML::Simple returns a hash, which has no particular order. There are multiple ways around this, if order is important to you.

  • You could use the ForceArray=>[names] option to put the names entities in an array, which should preserve their order, or
  • You might try XML::Twig so you can capture the items in order with your callback. This will have the advantage that you can process files much larger than you could hold in RAM.

    ...roboticus

    Update: Added code tags around ForceArray bit to fix square quotes...

  • Replies are listed 'Best First'.
    Re^2: sort order of imported xml data?
    by varian (Chaplain) on Mar 30, 2007 at 16:06 UTC
      It is not enough to use ForceArray in this case, because the XML that the OP presents uses key attributes. The regular elements will be forced into an array structure but the key attributes would still live in an unsorted hash.

      The solution is to combine ForceArray with the option KeyAttr to also force key attributes into an array.

      my $config = XMLin("meep.xlf", ForceArray => 1, KeyAttr =>[]);

      NB: This changes the structure that XML::Simple returns so you will need to update your code a little.

      I tend to use Data::Dumper to inspect the hash that XML::Simple returns to check if that meets my needs.

        ++varian:

        ...and that's what I get for guessing instead of trying it out.

        Thanks for the catch! (Yarch, two slipped gears on the same day. I'll have to be sure not to skip my morning coffee ... um, err, uh ....oil! Yeah, that's the ticket.....)

        ...roboticus

        Sorry for the late replies, work got over-busy and this project set on the backburner, but i'm back on it and a good step ahead thanks to your input.

        And i have to say, as for quickly solving my problem with the least effort, varian is right on spot! :)
        I should've known that i should've read the documentation more closely. ^^

        Also, yes, i've been basically using Data::Dumper all the time, but cut it out of my example code as not relevant. I however use it to basically reverse construct my access code. =)