Hi,
I am trying to loop through nested repeated elements in my config file but I am unable to find the number of nested elements:
Sample XML File:
<opt>
<person firstname="Joe" lastname="Smith">
<email>joe@smith.com</email>
<email>jsmith@yahoo.com</email>
</person>
<person firstname="Bob" lastname="Smith">
<email>bob@smith.com</email>
</person>
</opt>
Perl Code:
use XML::Simple;
use strict;
use Data::Dumper;
my $config = XMLin("testFile.xml",forcearray => ['person','email']);
my @persons = $config->{person};
print "Size:".@persons;
On running I always get the size as 1. I think the way I am trying to get the size is wrong because in order to get the person blocks I need to do $config->{person}->[0] etc. instead of $config->{person}[0]. I admit I have not used hashes earlier but I did try keys function on $config->{person} which gives "Type of arg 1 to keys must be hash (not hash element)" error. Could anyone please tell me the right way to loop through the elements?
Regards,
Ranjan
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.