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

Hi,
I am parsing a XML file using XML::Simple. It return a structure $configitem->{'config'}. It looks like this
$VAR1 = \[ { 'content' => 'Blocking', 'type' => 'Blocking' }, { 'content' => 'P01', 'type' => 'Priority' } ];
i am processing this structure to build HoA (%qryhash). I have listed the code below. in the foreach loop is executed only once. I need the following
print Dumper(\%qryhash); $VAR1 = { 'Blocking' => [ 'Blocking' ], 'Priority' => [ 'P01' ] };
But i am getting only
print Dumper(\%qryhash); $VAR1 = { 'Blocking' => [ 'Blocking' ] };
Here is the code snippet.
sub extract_table_queries{ my $configitem = $_[0]; my @assigneearray = (); my %qryhash; print ref $configitem->{'config'}; if (ref $configitem->{'config'} eq 'ARRAY'){ @queries = @{$configitem->{'config'}}; $i = 0; $count = @queries; foreach $q (@{$configitem->{'config'}}){ my $value = $q->{'content'}; my $param = $q->{'type'}; #$value =~ s/^\s+|\s*$//g; $param = &return_param_name($param); push @{$qryhash{$param}}, $value; if (lc $param eq lc $configitem->{rollup}){ push @rolluplist,$value; } my $tmp = sprintf("%-15s",$param); print "------>$tmp: $value\n"; } print Dumper(\%qryhash); print Dumper(\$configitem->{'config'}); exit; return(\%qryhash); } }
Can anyone please find out the mistake in my code and help me out?

Replies are listed 'Best First'.
Re: Problem iterating AoH
by bellaire (Hermit) on Nov 17, 2009 at 13:03 UTC
    Your code works just fine for me, except that I had to define a return_param_name method to return its own first argument. Assuming that method doesn't do anything bizarre, your code should work as given. Here's my complete output:
    ARRAY------>Blocking : Blocking ------>Priority : P01 $VAR1 = { 'Blocking' => [ 'Blocking' ], 'Priority' => [ 'P01' ] }; $VAR1 = \[ { 'content' => 'Blocking', 'type' => 'Blocking' }, { 'content' => 'P01', 'type' => 'Priority' } ];
Re: Problem iterating AoH
by biohisham (Priest) on Nov 17, 2009 at 15:20 UTC
    What is the $param returned for each 'type' key after the subroutine &return_param_name? I'd suggest you sprinkle some print statements around as follows and show what &return_param_name looks like,
    my $value = $q->{'content'}; my $param = $q->{'type'}; print $param,"$/"; #<------------------Here #$value =~ s/^\s+|\s*$//g; $param = &return_param_name($param); print $param,"$/"; #<--------------------Here push @{$qryhash{$param}}, $value;
    Since your problem lays in converting an AoH to a HoA by iterating over the AoH, using the Dumped output you provided it is possible to iterate as follows:
    #!/usr/local/bin/perl #title "Problem iterating AoH" use strict; use warnings; my @queries = ( { 'content'=>'Blocking', 'type' => 'blocking' }, { 'content'=>'p01', 'type'=>'Priority' } ); my %hash; for my $reference (@queries){ push @{$hash{$reference->{'type'}}},$reference->{'content'}; } use Data::Dumper; print Dumper(\@queries),"\n"; print Dumper(\%hash);
    OUTPUT:
    $VAR1 = [ { 'content' => 'Blocking', 'type' => 'blocking' }, { 'content' => 'p01', 'type' => 'Priority' } ]; $VAR1 = { 'blocking' => [ 'Blocking' ], 'Priority' => [ 'p01' ] };


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.