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

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

How would I print this arbitrarily large HoH
$HoH{$counter} = { holder => [@newholder], individual => [ @newind ], chromasome1 => [ @newchroma ], chromasome2 => [ @newchromb ], };
Such that I get this output
counter: 1 Holder: 1 chroma:val chromb:val counter: 1 Holder: 2 chroma:val ... counter: 1 Holder: 3 chroma:val ... ... ... ... counter: 2 Holder: 1 ... counter: 2 Holder: 2 counter: 2 Holder: 3 ... ...
You get the idea. The only advice I get for code returns a stringafied array ARRAY(0x1808dbc)

Replies are listed 'Best First'.
Re: Print HoH
by johngg (Canon) on Nov 09, 2007 at 15:50 UTC
    The ARRAY(0x1808dbc) should give you the clue that you are printing the value of the array reference (the array's address in the perl interpreter's memory space, if you will) rather than the array contents. You need to de-reference it using the @{ array-reference } syntax to get at the contents. Here's a simple example.

    use strict; use warnings; my %HoH = ( fred => { age => 54, kids => [ qw{ janet john mary } ] }, pete => { age => 37, kids => [ qw{ anne tim } ] }, ); foreach my $person ( keys %HoH ) { print qq{Person: $person\n}, qq{ Age: $HoH{$person}->{age}\n}, qq{ Kids: @{ $HoH{$person}->{kids} }\n}, ; }

    Which produces

    Person: pete Age: 37 Kids: anne tim Person: fred Age: 54 Kids: janet john mary

    I hope this helps you.

    Cheers,

    JohnGG

      Sure does help. Thanks very much.
Re: Print HoH
by toolic (Bishop) on Nov 09, 2007 at 15:27 UTC
      ... or even print Dumper \%HoH;