Question on refs inside hash

Discussion in 'Perl Misc' started by rocknmetal20, Apr 8, 2004.

  1. rocknmetal20

    rocknmetal20 Guest

    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
    rocknmetal20, Apr 8, 2004
    #1
    1. Advertising

  2. rocknmetal20

    Bob Walton Guest

    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
    Bob Walton, Apr 8, 2004
    #2
    1. Advertising

  3. (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\\
    Brian McCauley, Apr 8, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Nagle
    Replies:
    3
    Views:
    291
    John Nagle
    Feb 25, 2007
  2. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  3. Tony N.

    Printing an array of hash refs

    Tony N., Sep 22, 2004, in forum: Perl Misc
    Replies:
    11
    Views:
    207
    Tony N.
    Sep 24, 2004
  4. Alfred Z. Newmane
    Replies:
    6
    Views:
    105
    Mark Clements
    May 2, 2005
  5. Bryan
    Replies:
    6
    Views:
    200
    -berlin.de
    Apr 11, 2007
Loading...

Share This Page