FAQ
Hello,

Assuming I have reference to an hash array $rHash, what would be the
way to dereference it.
Would it be: %hashEntry = %{$rHash}; ?

Regards,
Sharan

Search Discussions

  • Robert Wohlfarth at Aug 19, 2010 at 12:09 pm

    On Thu, Aug 19, 2010 at 6:51 AM, Sharan Basappa wrote:

    Assuming I have reference to an hash array $rHash, what would be the
    way to dereference it.
    Would it be: %hashEntry = %{$rHash}; ?
    Yes, that dereferences the hash. It's useful notation in loops...
    foreach (keys %{$rHash})
    while (my ($key, $value) = each %{$rHash})

    To access a single entry, you can do something like this: $value =
    $rHash->{'field'};

    --
    Robert Wohlfarth
  • Chas. Owens at Aug 19, 2010 at 12:29 pm

    On Thu, Aug 19, 2010 at 07:51, Sharan Basappa wrote:
    Hello,

    Assuming I have reference to an hash array $rHash, what would be the
    way to dereference it.
    Would it be: %hashEntry = %{$rHash}; ?
    snip

    The ways to dereference a hash are

    # treat $ref as a hash variable
    %$ref

    # treat $ref as a hash variable, useful for when the reference is not
    a simple scalar
    %{$ref}

    #index into $ref
    $ref->{key}

    # bulkier way of indexing into $ref, the -> operator is preferred
    ${$ref}{key}

    # take a slice of $ref
    @{$ref}{qw/k1 k2 k3/}

    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Sharan Basappa at Aug 19, 2010 at 3:52 pm
    wow!

    My impending question about usage of -> also got answered ...
    On Thu, Aug 19, 2010 at 5:59 PM, Chas. Owens wrote:
    On Thu, Aug 19, 2010 at 07:51, Sharan Basappa wrote:
    Hello,

    Assuming I have reference to an hash array $rHash, what would be the
    way to dereference it.
    Would it be: %hashEntry = %{$rHash}; ?
    snip

    The ways to dereference a hash are

    # treat $ref as a hash variable
    %$ref

    # treat $ref as a hash variable, useful for when the reference is not
    a simple scalar
    %{$ref}

    #index into $ref
    $ref->{key}

    # bulkier way of indexing into $ref, the -> operator is preferred
    ${$ref}{key}

    # take a slice of $ref
    @{$ref}{qw/k1 k2 k3/}

    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedAug 19, '10 at 11:51a
activeAug 19, '10 at 3:52p
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase