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. Advertisements

  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. Advertisements

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. HANM
    Replies:
    2
    Views:
    1,516
    Joseph Kesselman
    Jan 29, 2008
  2. rp
    Replies:
    1
    Views:
    860
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    999
    David A. Black
    Jul 2, 2008
  4. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    364
    Steve
    Sep 28, 2003
  5. Replies:
    14
    Views:
    437
    Tomi Häsä
    Jan 10, 2005
  6. Arvin Portlock
    Replies:
    6
    Views:
    306
    Arvin Portlock
    Sep 2, 2005
  7. S P Arif Sahari Wibowo

    a reference to an element of an array or hash, how to create?

    S P Arif Sahari Wibowo, Aug 5, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    180
    S P Arif Sahari Wibowo
    Aug 5, 2008
  8. Replies:
    14
    Views:
    324
    C.DeRykus
    Dec 22, 2009
Loading...