Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

accessing elements of reference to hash of arrays

by johnguillory (Initiate)
on Jul 10, 2014 at 20:28 UTC ( [id://1093117]=perlquestion: print w/replies, xml ) Need Help??

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:
for $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] “; } }
Thanks for any clues. John

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
    > I’m failing to do this inside the function.

    Could you please be more specific?

    Did you check the passed ref with Data::Dumper to see if its populated?

    Did you get the keys printed?

    What is "failing"?

    Cheers Rolf

    (addicted to the Perl Programming Language)

    update

    Does your example even compile? I count (at least) one missing closing bracket.

      LanX, I found my mistake after trying dumper. I wasn't even passing the correct hash into the function. Geez. Thank you! John
Re: accessing elements of reference to hash of arrays
by Laurent_R (Canon) on Jul 10, 2014 at 20:45 UTC
    Hi, I would suggest, for a start, that you change this part:
    } elsif (ref($aref) eq “HASH”){ #This foreach of the hash ref doesn't print anything! foreach $key ( keys %{$aref} ){
    to something like this:
    } elsif (ref($aref) eq “HASH”){ print "Entering the hashref branch: \n"; foreach $key ( keys %{$aref} ){ # ... } else { print "Not entering the hashref branch: ", ref($aref), "\n +";
    At least, this will give you a disgnostic from where to continue. Or, else, you might also use the Perl debugger. Or the Data::Dumper module.
Re: accessing elements of reference to hash of arrays
by kcott (Archbishop) on Jul 11, 2014 at 14:13 UTC

    G'day John,

    Look very carefully at the double-quotes in the code that you posted.

    Compare:

    $ perl -Mstrict -Mwarnings -le 'my $x = []; print ref($x) eq "ARRAY" ? + q{aref} : q{other}' aref

    with:

    $ perl -Mstrict -Mwarnings -le 'my $x = []; print ref($x) eq “ARRAY” ? + q{aref} : q{other}' Unrecognized character \xE2; marked by <-- HERE after ef($x) eq <-- HE +RE near column 30 at -e line 1.

    Perhaps the editor you're using has some sort of auto-correct or smart-quote mechanism configured.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1093117]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-19 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found