johnguillory has asked for the wisdom of the Perl Monks concerning the following question:
Hello, The following code successfully prints all content of a hash of arrays:
Thanks for any clues. Johnfor $x ( keys %HoA ) { print "$x: "; for $i ( 0 .. $#{ $HoA{$x} } ) { print "$HoA{$x}[$i] "; } } Output looks like this: Keys: array 8001: bp ne sc ax br le no ns mn sl lt sh lw ni lu dl 5022: bp ne sc ax br le no ns mn sl lt sh lw ni lu dl 1018: bp ne br no lt lw ni l I pass the %HoA to a function and want to print all the elements of th +e %HoA—but I’m failing to do this inside the function. This is my pro +blem. (The same function that receives the HoA must also process an +array. ) some_function(\%HoA); # could be some_function(\@array) sub some_function{ $aref = shift; if (ref($aref) eq “ARRAY”){ Do something with array---I got this muchworking! } elsif (ref($aref) eq “HASH”){ #This foreach of the hash ref doesn't print anything! foreach $key ( keys %{$aref} ){ print “key: $key…..”; for ( $i = 0; $i <=100; $i++ ){ print “ $aref->{$key}[$i] “; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: accessing elements of reference to hash of arrays
by LanX (Saint) on Jul 10, 2014 at 20:40 UTC | |
by johnguillory (Initiate) on Jul 10, 2014 at 21:36 UTC | |
Re: accessing elements of reference to hash of arrays
by Laurent_R (Canon) on Jul 10, 2014 at 20:45 UTC | |
Re: accessing elements of reference to hash of arrays
by kcott (Archbishop) on Jul 11, 2014 at 14:13 UTC |
Back to
Seekers of Perl Wisdom