develooper Front page | perl.beginners | Postings from February 2002

Re: Dereferencing a referenced hash.

Thread Previous
From:
Tor Hildrum
Date:
February 28, 2002 05:31
Subject:
Re: Dereferencing a referenced hash.
Message ID:
B8A3EFC0.5766%torhildr@mac.com
> The trick is to use {$ref} where you would normally put the variable name:

print "${$rhash}{1}\n"; worked like a charm :)
 
> So $array[0] becomes ${$ref}[0] (not @$ref[0] which is a "slice").
> And $hash{key} becomes ${ref}{key}.

It seems somewhat inconsistent to me.
While I can dereference slices from $variables and @arrays this way, I can't
from a %hash? (using $$name, @$name[] and %$name{}).
Maybe it's my lack of understanding hash'es..
 
> Or you can use the little arrow syntax...
> 
> ${$ref}[0] is the same as $ref->[0].
> And ${$ref}{key} is the same as $ref->{key}.

Yes, this looks like the way to go. Much easier and clearer than the
previous syntax.

${$rhash}{1} == $rhash->{1}; ## both are scalar variables, not slices?
 
> In your script you are doing a variation of the first, where you are just
> dropping the curly braces.  In which case it would look like this:
> 
> ${$ref}[0] is the same as $$ref[0] (NOT @$ref[0]).
> ${ref}{key} is the same as $$ref{key}.
> 
> Hope that helps.  Check out the perlreftut manpage, it explains all of this
> pretty well while avoiding all of the nasty details of refs.

Thanks. :)

-- 
T.



Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About