in reply to XML::Simple Not Sorting

I just searched for the word sort in your lengthy code and did not find a match. XML::Simple does not sort the data for you ... you have to sort it yourself:

use XML::Simple; use Data::Dumper; my $xml = XMLin(\*DATA); print Dumper $xml; $xml->{bar} = [ sort @{ $xml->{bar} } ]; print Dumper $xml; __DATA__ <foo> <bar>4</bar> <bar>1</bar> <bar>3</bar> <bar>2</bar> <bar>5</bar> </foo>
I recommend that next time, you trim your code down to the relevant parts, for example, CGI.pm has nothing to do with the problem, so you shouldn't bother posting those parts. Just post the relevant part ... which means you need to copy the script and trim out the stuff you don't need. 8 or 9 times out of 10, you will solve the problem yourself if you do this. :)
Yeah ... like that. :)

UPDATE: now that i can see the spot, try this foreach instead: (untested)

for my $this (sort {$a->{THIS} cmp $b->{THIS} } @{$xml->{policies}}) {
Where you replace THIS with the XML tag you want to sort by.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: XML::Simple Not Sorting
by bkiahg (Pilgrim) on Jun 25, 2004 at 19:29 UTC
    Thank you for the comments, I will certainly take them to heart. According to the documentation perldoc XML::Simple:



    Thank you for the code it will certainly come in handy.
Re^2: XML::Simple Not Sorting
by bkiahg (Pilgrim) on Jun 25, 2004 at 20:28 UTC
    Here's the datastructure that I'm tring to sort.
    $VAR1 = { 'policies' => [ { 'name' => 'ZAbc', 'link_name' => '7080-A103 zAbc.doc', 'description' => 'This is where the descript +ion goes.', 'policy_number' => '7080-A103' }, { 'name' => Def', 'link_name' => '7080-D100 ef.doc', 'description' => 'This is where the descript +ion goes.', 'policy_number' => '7080-D100' }, { 'name' => Pqrs', 'link_name' => '7080-E102 Pqrs.doc', 'description' => 'This is where the descript +ion goes.', 'policy_number' => '7080-E102' } ], 'title' => 'Test Policies', 'folder' => 'Title' };
    Shortened to get to the point.

      Try this:

      use Data::Dumper; for ( sort { $a->{policy_number} cmp $b->{policy_number}} @{ $VAR1->{p +olicies} }) { print Dumper $_; }
      Which yielded the following for me: (even though the data was already sorted by 'policy_number')
      $VAR1 = { 'name' => 'ZAbc', 'description' => 'This is where the description goes.', 'link_name' => '7080-A103 zAbc.doc', 'policy_number' => '7080-A103' }; $VAR1 = { 'name' => 'Def', 'description' => 'This is where the description goes.', 'link_name' => '7080-D100 ef.doc', 'policy_number' => '7080-D100' }; $VAR1 = { 'name' => 'Pqrs', 'description' => 'This is where the description goes.', 'link_name' => '7080-E102 Pqrs.doc', 'policy_number' => '7080-E102' };

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)