in reply to Having trouble looping through a data structure
The function ref can allow you to make many useful decision with respect to the type of dereferencing task appropriate for a given segment of the data structure, for example, if 'ref' returned 'ARRAY' when you are looping through a hash you could probably access that particular array by dereferencing it as
uncomment lines to see the returned value of 'ref' and compare that to the subsequent dereferencing approach chosen...foreach my $element (@{$hash{$key}}{ #work on $element; }
use strict; use warnings; use Data::Dump qw(pp); my %hash; push @{$hash{'article'}},{}; $hash{'article'}[0]= {'SKU'=> ['CDS0013'], 'InternalSKU'=>'179', 'Avai +lableItems'=>['100']}; $hash{'article'}[1]= {'SKU'=> ['CDS0014'], 'InternalSKU'=>'180', 'Avai +lableItems'=>['102']}; foreach my $key (keys %hash){ #print ref $hash{$key},"\n"; foreach my $element (@{$hash{$key}}){ # print ref $element,"\n"; $articleCount = scalar @{$hash{$key}}; #get the coun +t ... foreach my $innerKey(keys %{$element}){ print "$element->{$innerKey}","\n"; #some mor +e referencing yet } } } print "there are $articleCount articles\n"; print pp \%hash;
|
|---|