Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Print HoH

by BioNrd (Monk)
on Nov 09, 2007 at 15:20 UTC ( [id://649914]=perlquestion: print w/replies, xml ) Need Help??

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;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found