Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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]
herveus waves to virtualsue
erix waves to Everybody
herveus waves back to erix

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2017-09-26 14:02 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (296 votes). Check out past polls.