Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Looping through nested repeated elements using XML::Simple

by pemungkah (Priest)
on Dec 13, 2006 at 07:02 UTC ( #589502=note: print w/replies, xml ) Need Help??


in reply to Looping through nested repeated elements using XML::Simple

Well, let's do what we should do when we have a data structure we don't understand: look at it in the debugger.
$ perl -d xml_sample.pl Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(xml_sample.pl:5): my $config = XMLin("testFile.xml",forc +earray => ['person','email']); + DB<1> l 5==> my $config = XMLin("testFile.xml",forcearray => ['person','ema +il']); 6: my @persons = $config->{person}; 7: print "Size:".@persons; DB<1> c 6 # run continuously to line 6 main::(xml_sample.pl:6): my @persons = $config->{person}; DB<2> x $config 0 HASH(0x1988e88) 'person' => ARRAY(0x19acb00) 0 HASH(0x1988f30) 'email' => ARRAY(0x19acba8) 0 'joe@smith.com' 1 'jsmith@yahoo.com' 'firstname' => 'Joe' 'lastname' => 'Smith' 1 HASH(0x198a0cc) 'email' => ARRAY(0x19acab8) 0 'bob@smith.com' 'firstname' => 'Bob' 'lastname' => 'Smith' DB<3> q $
So we can see that the value of $config->{person} is actually a reference to an array, not an array itself. You'll have to dereference it to get an actual array. Try @{ $config->{person} }. The elements in the array created by this expression will be the two references to the hashes containing references to arrays containing the email values.

Replies are listed 'Best First'.
Re^2: Looping through nested repeated elements using XML::Simple
by ranjan_jajodia (Monk) on Dec 13, 2006 at 07:20 UTC
    Thanks pemungkah for the explaining the way to solve this kind of problem. I was using DataDumper to see the contents but this way I can do more things while debugging.
    Regards,
    Ranjan
      You're very welcome. One other thing: you can play around with expressions in the debugger as well. For instance, if you wanted to see what @{ $config->{person} } would give you, you could just enter x @{ $config->{person} } at the debugger prompt, and the debugger would happily dump it out for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://589502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2023-12-11 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?