http://qs1969.pair.com?node_id=319189

c has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash of arrays. I would like to create equivalents between hash keys without repeating the whole array.

my %hash = ( 'one' => [ ( 'abc', 'def', 'ghi' ) ] ); $hash{'two'} = @{$hash{'one'}};

Doesnt look as though this works, can someone educate me on this? Thanks -c

Replies are listed 'Best First'.
Re: one hash value (array) into another
by Enlil (Parson) on Jan 06, 2004 at 18:25 UTC

    Really close.

    What your hash ends up looking like is:

    %hash = ( 'one' => [ 'abc', 'def', 'ghi' ], 'two' => 3 );

    This is because $hash{'two'} is being assigned the length of the array held in $hash{'one'}. (in the same way that $array_size = @array would assign $array_size the length of @array)

    What I assuming you want is a hash that ends up like:

    %hash = ( 'one' => [ 'abc', 'def', 'ghi' ], 'two' => [ 'abc', 'def', 'ghi' ] );
    which can be accomplished by something like so:
    $hash{'two'} = [ @{$hash{'one'}} ];

    -enlil

Re: one hash value (array) into another
by duff (Parson) on Jan 06, 2004 at 18:29 UTC

    It all depends on what you mean by "equivalent". To copy the contents of the array, you would do

    $hash{'two'} = [ @{$hash{'one'}} ];

    To make sure that they both reference the same array, just

    $hash{'two'} = $hash{'one'};

    I assume that you mean the former though.

    Update: /me suddenly experiences a strange sense of deja vu and decides he needs to type faster or notice newer nodes sooner or both :-)

Re: one hash value (array) into another
by Limbic~Region (Chancellor) on Jan 06, 2004 at 18:26 UTC
    c,
    It depends on what you mean by "equivalents".

    If you mean having an initial copy that can diverge

    $hash{two} = [ @{$hash{one}} ];
    If you mean you want both keys to be referents to the same anonymous array
    $hash{two} = $hash{one};
    Cheers - L~R
Re: one hash value (array) into another
by dragonchild (Archbishop) on Jan 06, 2004 at 20:14 UTC
    Alternatively, you can do:
    @{$hash{two}} = @{$hash{one}};

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: one hash value (array) into another
by jbware (Chaplain) on Jan 06, 2004 at 18:25 UTC
    Here is one way to do it:
    map (push (@{$hash{'two'}}, $_), @{$hash{'one'}});
Re: one hash value (array) into another
by calin (Deacon) on Jan 20, 2004 at 19:26 UTC
    If you want different arrays, with the same scalar things inside, you can do:

    use Data::Dumper; $h{a} = [qw/x y z/]; $h{b} = &{sub {\@_}}(@{$h{a}}); $h{a}->[0] = 'new_x_is_shared'; push @{$h{a}}, 'pusher_is_not_shared'; print Dumper $h{a}; print Dumper $h{b};

    with the output:

    $VAR1 = [ 'new_x_is_shared', 'y', 'z', 'pusher_is_not_shared' ]; $VAR1 = [ 'new_x_is_shared', 'y', 'z' ];