http://www.perlmonks.org?node_id=998678


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.