Hello Monks,

I am trying to add a hash to a hash reference. Here is a snippet of the original xml data.
<policies> <New name="Test3" date="28-8-104" description="asdf" link_name="Test3.doc" /> <Newer name="Whatever" date="28-8-104" description="asdf" link_name="Whatever.doc" /> <policies2 name="General Admit to GPRMC" catagory="General" description="" link_name="General Admit to GPRMC.doc" /> </policies>

Here is the perl code I am using.
use strict; my $xs = new XML::Simple; my $xml = $xs->XMLin("$folder_path\\policies.xml", forcearray => 1, ke +yattr => ['policies']); my $hash_add = [{ $cat => [{ 'name' => "$name", 'link_name' => "$link_name", 'description' => "$desc", 'date' => "$today_date" }] }]; # Add to hash push (@{$xml->{'policies'}}, $hash_add); print "Content-type: text/html\n\n"; # Sort hash @{ $xml->{'policies'} } = sort { "\L$a->{name}" cmp "\L$b->{name}"} @{ + $xml->{'policies'} }; use Data::Dumper; print Dumper($xml); $xs->XMLout($xml, outputfile => "$folder_path\\policies.xml", attrinde +nt => 1);

This is the data dump that I come out with is.
'policies' => [ { 'New' => [ { 'date' => '28-8-104', 'name' => 'Test3', 'link_name' => 'Test3.doc', 'description' => 'asdf' } ], 'policies2' => [ { 'name' => 'General Admit +to GPRMC', 'link_name' => 'General A +dmit to GPRMC.doc', 'description' => '', 'catagory' => 'General' } ], 'Newer' => [ { 'date' => '28-8-104', 'name' => 'Whatever', 'link_name' => 'Whatever.doc' +, 'description' => 'asdf' } ] }, [ { 'Newer' => { 'date' => '28-8-104', 'name' => 'whatever', 'description' => 'something +', 'link_name' => 'whatever.do +c' } } ] ]

But my xml file looks like this.
<policies> <New name="Test3" date="28-8-104" description="asdf" link_name="Test3.doc" /> <Newer name="Whatever" date="28-8-104" description="asdf" link_name="Whatever.doc" /> <policies2 name="General Admit to GPRMC" catagory="General" description="" link_name="General Admit to GPRMC.doc" /> </policies> <policies> <anon name="whatever" date="28-8-104" description="something" link_name="whatever.doc" /> </policies>

My question is how do I get it so that it isn't adding the extra policies brackets. And how do I sort the catagories and the name inside of the catagories. I get the error "No such pseudo-hash field "name"" I apologize for the long post, but I wanted to be as thorough as I could. Any help is appreciated.
Thanks

In reply to Adding a hash to a hash reference. by bkiahg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.