djbryson has asked for the wisdom of the Perl Monks concerning the following question:

I have multi level XML
I figured out how to extract whatever parts I want like so:
print $data->{channel}{item}[3]{title}; or print $data->{channel}->{item}[3]->{title};
How can I loop through this and output?
So, I have a hash called "channel" that contains an array called "item" which contains another hash called "title" correct? So, if I want to loop all the titles, what's the syntax?

Replies are listed 'Best First'.
Re: XML - Simple - loop
by Corion (Patriarch) on Sep 23, 2008 at 18:41 UTC
Re: XML - Simple - loop
by ikegami (Patriarch) on Sep 23, 2008 at 19:51 UTC
    my $items = $data->{channel}{item}; for my $item ( @$items ) { print( $item->{title}, "\n" ); }
Re: XML - Simple - loop
by northwind (Hermit) on Sep 23, 2008 at 19:27 UTC

    1. Do what Corion suggested and read References Quick Reference.
    2. After reading the above quick reference, the code foreach my $key (keys %{$hash_ref}) will mean something to you and be your friend (if you are not sure what that line does, check out the perldocs).

Re: XML - Simple - loop
by MadCat (Initiate) on Sep 24, 2008 at 05:35 UTC
    print $item->{'title'} for(@{$data->{'channel'}->{'item'}});
    Something like that oughta get you on your way :)