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


in reply to Problem handling array from XML stream

First things first - I get a not well-formed error when I try to run your code. The fix is to quote the value of PROGRAM_COUNT in the Participant tag (PROGRAM_COUNT="3"). (Actually, that's the second error I got. The first thing I did was add use strict; use warnings; and then declare all of the variables.)

On to business. From what I can tell, you're getting confused about how to use references.

For example, in the following line you are assigning an array reference to an array, which is probably not what you intended to do.

my @program_array = $XMLref->{Response}->[0]->{Program};
Either expand (dereference) the array ref to an array when you assign it, or simply assign the reference.
my @program_array = @{ $XMLref->{Response}[0]{Program} }; my $program_aref = $XMLref->{Response}[0]{Program};

Secondly, you've got a typo in $programs->{status};. This key is capitalized in the XML ('Status').

Update: Thirdly, see Thelonius' reply to this node.

Here is a corrected version of the code:

if( ref( $XMLref->{Response}[0]{Program}[0] ) ) { my $program_arrayref = $XMLref->{Response}[0]{Program}; # Walk through the programs foreach my $programs_href ( @{ $program_arrayref } ) { my $status_aref = $programs_href->{Status}; my $one_program_name = $programs_href->{NAME}; # Walk through the statuses within each program foreach my $status_href ( @{ $status_aref } ) { my $hold_desc = $status_href->{StatusDesc}[0]; my $hold_start = $status_href->{StatusDate}[0]; my $hold_end = $status_href->{StatusEndDate}[0]; # a lot of other processing here print join( ':', $one_program_name, $hold_desc, $hold_start, $hold_end ), "\n"; } } }

This prints:

Balance:nl1 has been published:04/20/05: Balance:mas has been published:04/20/05:04/26/06 Breathe:nl1 has been published:04/20/05: Breathe:mas has been published:04/20/05:04/26/06

The following links may help:

HTH

Update:Thanks to Thelonius for pointing out a correction to the code that I made but neglected to mention. I guess I was fixing faster than I was documenting. :-)