in reply to Getting keys/values from a referenced hash
In general, if you've got a reference to a hash - let's call it $ref - then you can work with it like this:
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.#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
hth,
andy.
update: and the book "Effective Perl Programming" has an excellent explanation, very full, with pictures.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: dereferencing syntax Re: Getting keys/values from a referenced hash
by professa (Beadle) on Nov 28, 2001 at 15:40 UTC | |
by andye (Curate) on Nov 28, 2001 at 16:02 UTC | |
by professa (Beadle) on Nov 28, 2001 at 17:55 UTC | |
by blakem (Monsignor) on Nov 28, 2001 at 18:26 UTC | |
by professa (Beadle) on Nov 28, 2001 at 19:07 UTC | |
| |
by professa (Beadle) on Nov 28, 2001 at 18:04 UTC | |
by professa (Beadle) on Nov 28, 2001 at 18:16 UTC |