Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reference to hash element

Thread Tools

Reference to hash element

Konrad Eisele
Posts: n/a
is is possible to reference a element inside a hash?
something like:

%h = ( a => 1);
$a = \%h{'a'};
$a = 2;
=> $h{'a'} would be 2

Reply With Quote
Dave Weaver
Posts: n/a
Konrad Eisele <(E-Mail Removed)> wrote:
> is is possible to reference a element inside a hash?
> something like:
> %h = ( a => 1);
> $a = \%h{'a'};

The hash element is $h{'a'}, so to take a reference to it:

$a = \$h{'a'}

Now $a is a reference to the hash element. To assign to the
referenced element you must dereference $a by using an extra
'$' prefix:

$$a = 2;

Alternatively, you could use a for() to create a temporary
alias to the element:

for my $a ( $h{'a'} ) {
$a = 2;

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Combining multiple hash references into one hash reference Arvin Portlock Perl Misc 6 09-02-2005 07:18 PM
copying values from a hash into via tied hash reference Perl Misc 14 01-10-2005 01:22 PM
hash reference as a hash key Steve Perl Misc 6 09-28-2003 10:49 AM