Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: printing contents of a hash

by davido (Cardinal)
on Apr 05, 2021 at 15:57 UTC ( [id://11130849]=note: print w/replies, xml ) Need Help??


in reply to printing contents of a hash

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"; }

...or...

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:

I edited your post, which had only raw text, no formatting such as <p>...</p> or <code>...</code>:

Hi All I have a HASH built likr 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_coe, how do I +do that?

To it, I added paragraph and code tags anywhere you had either a line break or code:

<p>Hi All </p> <p> I have a HASH built likr this </p> <c> { . my @theta = $reg->theta(); $reg_coes{$id} = $theta[1]; $reg->print(); } return \%reg_coes; </c> <p> what I want to do is print out the key and value in reg_coe, how do I +do that? </p>

Now your post is legible. You can read how to do this at Writeup Formatting Tips.


Dave

Replies are listed 'Best First'.
Re^2: printing contents of a hash
by merrittr (Novice) on Apr 07, 2021 at 02:06 UTC
    Thanks Dave that did the trick!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found