Question on refs inside hash

R

rocknmetal20

Hi all,

Is there a way to refer other elements of the hash inside that hash
WHILE I am defining the hash.

Example: I have a hashref with 'x' mapping to an array ref. To save
memory or for someother reason I want to use that same value for 'y'
too.

For the following,
my $a = {
'x' => ['a'],
'y' => ['a'],
};
Instead duplicating the value for y, I want to refer to value for key
'x'.
I can do
my $a = {
'x' => ['a'],
};

$a->{'y'} = $a->{x};

But I want to know, if it can be done while defining the hash.

This does not seem to work:
my $a = {
'x' => ['a'],
'y' => $VAR1->{x}
};

print Dumper($a);

$VAR1 = {
'x' => [
'a'
],
'y' => undef
};


Thanks
Rock
 
B

Bob Walton

rocknmetal20 wrote:

....
Is there a way to refer other elements of the hash inside that hash
WHILE I am defining the hash.

Example: I have a hashref with 'x' mapping to an array ref. To save
memory or for someother reason I want to use that same value for 'y'
too.

For the following,
my $a = {
'x' => ['a'],
'y' => ['a'],
};
Instead duplicating the value for y, I want to refer to value for key
'x'.
I can do
my $a = {
'x' => ['a'],
};

$a->{'y'} = $a->{x};

But I want to know, if it can be done while defining the hash.


Try:

my $arrayref=['a'];
my $a={x=>$arrayref,y=>$arrayref,};

Note carefully that $a->{x} and $a->{y} then refer to *exactly the same
array*, and that a statement like:

$a->{x}->[0]='b';

will result in:

print $a->{y}->[0];

printing "b".

This does not seem to work:
my $a = {
'x' => ['a'],
'y' => $VAR1->{x}

???--------------^^^^
Where was $VAR1 defined? And, if you had used $a instead of $VAR1, it
wouldn't have worked either, because the order of evaluation calls for
the anonymous hash reference to be built first, then assigned to $a, so
$a wouldn't exist during the time the anonymous hash is being built.

};

print Dumper($a);

$VAR1 = {
'x' => [
'a'
],
'y' => undef
};
....


Rock
 
B

Brian McCauley

Is there a way to refer other elements of the hash inside that hash
WHILE I am defining the hash.

Example: I have a hashref with 'x' mapping to an array ref. To save
memory or for someother reason I want to use that same value for 'y'
too.

For the following,
my $a = {
'x' => ['a'],
'y' => ['a'],
};

You are not alaysing your problem correctly. The stuff inside the { }
anon hash constructor is just a list context expression. Until
evaluation of that expression is complete then there's no hash
involved.

So your question has nothing to do with hashes. Your question is can
I refer in an expression to an earlier part of the same expression.
The answer is that you can't without using a variable. Unfortunately
because the scope of a variable declaration starts at the next
statement the syntax gets a bit messy.


my $a = do {
my $t;
+{
x => ($t = ['a']),
y => $t,
};
};

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top