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

Re: [MacPerl] Dereferencing question



At 7:46 AM 11/22/00, Andrew O. Mellinger wrote:
>Greetings,
>
>I know this is the MacPerl list, but I would like to post a pure 
>perl question.
>
>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.

You got close. I like to use extra-braces way of accessing 
references. Below are your tries, commented out, then a solution:

#!perl -w

my $hRef = {};
my $aRef = [];

$$hRef{'alpha'} = 1;
$$hRef{'beta'} = 2;
$$hRef{'gamma'} = 3;

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";

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

%h = %{ $hRef };
for (keys %h) { print $_, " => $h{$_} \n" }

__END__

prints:

gamma => 3
beta => 2
alpha => 1
array => ARRAY(0x593b28c)

You then have a second level of de-referencing to do to get the 
contents of the stored array.

1;

- Bruce

__Bruce_Van_Allen___Santa_Cruz_CA__

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