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

Hello Monks,

I'm having trouble getting XML::Simple to sort my data. I'm trying to edit a config xml file and it doesn't sort the new name of the policy in the config file. Any ideas as to what I'm doing wrong?
#! C:\Perl\bin\perl.exe -w use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use XML::Simple; use File::Copy; my $path = "C:\\web\\policies\\"; my $folder; my($old_file_name, $old_file, $policy_number, $name, $desc, $link_name +, $bytesread, $buffer); ############################## # Open xml document and edit # ############################## if ($file) { $link_name = $policy_number . " " . $name . '.doc' } else { $link_name = $old_file } my $xs = new XML::Simple; my $xml = $xs->XMLin("$folder_path\\policies.xml", forcearray => 1, ke +yattr => ['policies']); foreach my $this (@{$xml->{policies}}) { if ($this->{name} eq $old_file_name) { $this->{link_name} = $link_name; $this->{description} = $desc; $this->{policy_number} = $policy_number; $this->{name} = $name; last; } } $xs->XMLout($xml, outputfile => "$folder_path\\policies.xml", attrinde +nt => 1);
Everything works except the sorting. BTW I'm using activeperl 5.8 810 with xml::simple 2.12. Thanks in advance!

Update: Trimmed down.

Update2: Big thanks to jeffa! 1. For bearing with me and 2. For the relevant code.
@{ $xml->{policies} } = sort { $a->{name} cmp $b->{name}} @{ $xml->{policies} };

Replies are listed 'Best First'.
Re: XML::Simple Not Sorting
by jeffa (Bishop) on Jun 25, 2004 at 19:06 UTC

    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)
    
      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.
      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)