Reference to hash element

Discussion in 'Perl Misc' started by Konrad Eisele, Aug 11, 2005.

  1. 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
     
    Konrad Eisele, Aug 11, 2005
    #1
    1. Advertising

  2. Konrad Eisele

    Dave Weaver Guest

    Konrad Eisele <> 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;
    }
     
    Dave Weaver, Aug 11, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    597
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    677
    David A. Black
    Jul 2, 2008
  3. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    144
    Steve
    Sep 28, 2003
  4. Replies:
    14
    Views:
    273
    Tomi Häsä
    Jan 10, 2005
  5. Arvin Portlock
    Replies:
    6
    Views:
    152
    Arvin Portlock
    Sep 2, 2005
Loading...

Share This Page