Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998678]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-02-26 00:15 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (369 votes). Check out past polls.