[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Dereferencing question



On Wed, Nov 22, 2000 at 07:46:13AM -0800, Andrew O. Mellinger wrote:
> Greetings,
> 
> I know this is the MacPerl list, but I would like to post a pure perl question.
> 

There is also a macperl-anyperl list, which is appropriate for general Perl
questions such as this.


> I have some nested hash/array structures and I am having trouble 
> dereferencing them all at once.  Up until now, I've been doing it in 
> stages, but I'd like to understand how to do it in one fell swoop.
> 
> So I have:
> 
> my $hRef = {};
> my $aRef = [];
> 
> $$hRef{'alpha'} = 1;
> $$hRef{'beta'} = 2;
> $$hRef{'gamma'} = 3;

I find that

$hRef->{'alpha'} = 1;

is clearer.


> push @$aRef, "One";
> push @$aRef, "Two";
> push @$aRef, "Three";
> 
> $$hRef{'array'} = $aRef;
> 
> 
> Now, I want to push another value onto the embedded array.  I've 
> tried all of the following, but none seem to work
> 
> push $$hRef{'array'}, "Four";
> push @$hRef{'array'}, "Four";
> push @($hRef{'array'}), "Four";
> push @($$hRef{'array'}), "Four";

Braces are always used to disambiguate variable names and references,
rather than parentheses.

push @{$hRef->{'array'}}, "Four";

Your second attempt, @$hRef{'array'}, is parsed as @{$href}{'array'}, which
is a hash slice.


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org