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


In reply to Re: printing contents of a hash by davido
in thread printing contents of a hash by merrittr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.