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} };
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.