dereferencing hash arrays

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

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sharan Basappa [ Do, 19 August 2010 13:51 ] [ ID #2046341 ]

Re: dereferencing hash arrays

--0022158bfd91641c65048e2c0db6
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Aug 19, 2010 at 6:51 AM, Sharan Basappa <sharan.basappa [at] gmail.com>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

--0022158bfd91641c65048e2c0db6--
Robert Wohlfarth [ Do, 19 August 2010 14:09 ] [ ID #2046343 ]

Re: dereferencing hash arrays

On Thu, Aug 19, 2010 at 07:51, Sharan Basappa <sharan.basappa [at] gmail.com> 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
[at] {$ref}{qw/k1 k2 k3/}

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

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Do, 19 August 2010 14:29 ] [ ID #2046345 ]

Re: dereferencing hash arrays

wow!

My impending question about usage of -> also got answered ...

On Thu, Aug 19, 2010 at 5:59 PM, Chas. Owens <chas.owens [at] gmail.com> wrote:
> On Thu, Aug 19, 2010 at 07:51, Sharan Basappa <sharan.basappa [at] gmail.com> 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
> [at] {$ref}{qw/k1 k2 k3/}
>
> --
> Chas. Owens
> wonkden.net
> The most important skill a programmer can have is the ability to read.
>

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sharan Basappa [ Do, 19 August 2010 17:51 ] [ ID #2046350 ]
Perl » gmane.comp.lang.perl.beginners » dereferencing hash arrays

Vorheriges Thema: Strange behaviour while using DBI with binding
Nächstes Thema: Perl modules