And for my second post.... I am coming unstuck on Hashes and Arrays. I am using XML:Simple to read in an xml file. In order to read my tree I have to set a flag 'forcearray'. The tree structure is like this:
<root> <page> <field> <ID></ID> <value></value> </field> </page> </root>
The root can have more many pages, and each page can have many fields. The fields only have the ID and value. I am trying to add a new field to a specific page. So far I have got:
my $pageobj=$data->{page}[$pagenumber]; my $fieldobj=$pageobj->{field};
when dumped this produces:
[ { 'value' => [ 'test' ], 'ID' => [ '0000001' ] }, { 'value' => [ 'test' ], 'ID' => [ '0000002' ] }, { 'value' => [ 'test' ], 'ID' => [ '0000003' ] }, { 'value' => [ 'test' ], 'ID' => [ '0000004' ] } ]
I have tried various ways of adding a new field to this, I would ideally like to build a new field from values I enter, and then add it to the array. I tried creating a new hash, and adding in a value and ID. Although I feel I am slipping up somewhere:
my $pageobj=$data->{page}[$pagenumber]; my $fieldobj=$pageobj->{field}; my %tmpF=(); my @tmpA; push(@tmpA,"0001"); %tmpF->{ID}=@tmpA; push(@$fieldobj,%tmpF);
After dumping this, it doesnt seem to have the desired result... Any help would be amazing TienLung

In reply to Arrays and Hashes woes by TienLung

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.