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

Hi,

This stuff really does my head in :-), can anyone help please?

If I can access each of the values in a multi-dimensional array like this:

$xml->{event}->[0]->{venue_code} $xml->{event}->[1]->{venue_code} $xml->{event}->[2]->{venue_code}

How can I loop through all the array values, rather than specifying an index for each one?

I.e. something like this.

foreach my $event ($xml->{event}) { print "$event->{venue_code}<br/>"; #this doesnt work print "$event->[0]->{venue_code}<br/>"; #this works }

Thanks, Tom

Replies are listed 'Best First'.
Re: Looping a multi-dimensional array
by ikegami (Patriarch) on Oct 20, 2004 at 15:32 UTC

    You simply forgot to dereference the array:

    || | vv v foreach my $event (@{$xml->{event}}) { print "$event->{venue_code}<br/>"; }
      Cheers mate, I thought it was something like that but just couldn't remember the syntax... new it was simple though. I will try and remember this time ;-)
Re: Looping a multi-dimensional array
by Joost (Canon) on Oct 20, 2004 at 15:34 UTC
Re: Looping a multi-dimensional array
by tye (Sage) on Oct 21, 2004 at 02:25 UTC