Hi there professsa,

In general, if you've got a reference to a hash - let's call it $ref - then you can work with it like this:

#construct a ref to an anonymous hash: my $ref = {andy => 'likes cake'}; #note *curly* brackets #another way my %hash = (andy => 'likes beer too'); $ref = \%hash; #normal brackets + of course #get at one specific value print $ref->{andy}; #another way print $$ref{andy}; #get all the keys print keys %$ref; #get all the values print values %$ref; #get key/value pairs while (my ($key, $val) = each %$ref) {print "$key = $val"} #copy the hash into a new hash my %newhash = %$ref #if you use the -> syntax with references to references, then you can +omit the ->s, after the first one, like this: my $ref = { andy => {eats => 'cake'}}; print $ref->{andy}{eats}; #note only one -> necessary
More info can be found here: perlref or here: References quick reference (or, newly, here if you speak German: (lang: de) Referenzen) or by typing 'perldoc perlref' at your command line.

hth,
andy.

update: and the book "Effective Perl Programming" has an excellent explanation, very full, with pictures.


In reply to dereferencing syntax Re: Getting keys/values from a referenced hash by andye
in thread Getting keys/values from a referenced hash by professa

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.