Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: XML::Simple XML / XMLin / XMLout? or something else?

by greengaroo (Hermit)
on Oct 12, 2012 at 13:32 UTC ( #998678=note: print w/ replies, xml ) Need Help??


in reply to XML::Simple XML / XMLin / XMLout? or something else?

This structure is a HashRef that contains ArrayRef. When you see { } its a HashRef, when you see [ ] its an ArrayRef. To loop through them, you have to dereference them.

Lets say your reference variable is called $VAR1 (obviously this name comes from Data::Dumper so use the real variable). You can do this:

# Bellow, you have two dereference # The arrow dereferences the HashRef $VAR1 # The @{ } dereferences the ArrayRef under 'book' => foreach my $book ( @{ $VAR1->{'book'} } ) { # Then, each $book is another HashRef print $book->{'isbn'}, "\n"; print $book->{'title'}, "\n"; # But 'author' is an ArrayRef: foreach my $author ( @{ $book->{'author'} } ) { print "Author: ", $author, "\n"; } }

That does not print the XML code, of course, but nothing prevents you from reprinting the labels manually. Otherwise, you have to use an XML related module.

There are no stupid questions, but there are a lot of inquisitive idiots.


Comment on Re: XML::Simple XML / XMLin / XMLout? or something else?
Select or Download Code
Re^2: XML::Simple XML / XMLin / XMLout? or something else?
by Roboz (Novice) on Oct 12, 2012 at 14:21 UTC

    Thanks for the info! I was having a tough time looping through array refs and hash refs in the past. This is good stuff.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-09-02 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls