Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

printing contents of a hash

by merrittr (Novice)
on Apr 05, 2021 at 06:53 UTC ( #11130829=perlquestion: print w/replies, xml ) Need Help??

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

Hi All

I have a HASH built like this

{ . my @theta = $reg->theta(); $reg_coes{$id} = $theta[1]; $reg->print(); } return \%reg_coes;

what I want to do is print out the key and value in reg_coes, how do I do that?

Replies are listed 'Best First'.
Re: printing contents of a hash
by Athanasius (Archbishop) on Apr 05, 2021 at 07:32 UTC

    Hello merrittr,

    If you just want to print out a particular key/value pair, and you know the key, use something like print $key, ', ', $reg_coes{$key} (or $reg_coes->{$key} if $reg_coes is a hash reference). But I assume you want to print out the whole hash, in which case see the FAQ How do I process an entire hash? for various approaches.

    An alternative is to use a module such as Data::Dumper or (my usual preference) Data::Dump. For the latter, you just do:

    use Data::Dump; ... dd \%reg_coes;

    and the module does the hard work for you.

    Note that hashes are unsorted, so if you want to print out the contents in a particular order, you need to impose that order manually, as shown in the FAQ.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: printing contents of a hash
by davido (Cardinal) on Apr 05, 2021 at 15:57 UTC

    I guess we can assume that the code you posted takes place inside a subroutine, otherwise the return would be pointless. Your subroutine returns a reference to a hash. So to print it, you'll need to dereference. Assume your subroutine is named mysub:

    my $hash_ref = mysub(); while (my ($k, $v) = each(%$hash_ref)) { print "$k = $v\n"; }


    my $hash_ref = mysub(); foreach my $key (keys %$hash_ref) { print "$k = $hash_ref->{$k}\n"; }

    There are lots of other ways, but these are two that will work well for simple hashes. If your hash contains references to other entities, the solution will become more complicated. If all you want to do is just visualize the data structure, use Data::Dumper:

    use Data::Dumper; # ... my $hash_ref = mysub(); print Dumper $hash_ref;

    Read below if you want to know how your post came to be nicely formatted:


      Thanks Dave that did the trick!
Re: printing contents of a hash
by stevieb (Canon) on Apr 05, 2021 at 18:32 UTC
    what I want to do is print out the key and value in reg_coe

    You don't have any hash named reg_coe.

    Although this is a small example, it does pay to be detailed and clear in your questions.

      good catch Steve! reg_coes

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11130829]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2021-04-18 18:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found