Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question on refs inside hash

Reply
Thread Tools

Question on refs inside hash

 
 
rocknmetal20
Guest
Posts: n/a
 
      04-08-2004
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
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      04-08-2004
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


--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      04-08-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (rocknmetal20) writes:

> 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\\
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Traversing a hash with array refs as keys? Bryan Perl Misc 6 04-11-2007 11:55 AM
Are weak refs slower than strong refs? John Nagle Python 3 02-25-2007 08:54 PM
Multiple Inheritance: mixed base class refs (hash, array) Alfred Z. Newmane Perl Misc 6 05-02-2005 07:04 PM
Printing an array of hash refs Tony N. Perl Misc 11 09-24-2004 03:59 PM



Advertisments