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

Hi Monks,

Can you please guide me how to push an array into Hash of Array.

#!/usr/local/bin/perl use strict; use Data::Dumper; my @arr; my %hash; my @arr=['rose','orange','green']; $hash{"first"}=[@arr]; print STDERR Dumper(\%hash);

I want to push the following @newarr into %hash in same key

my @newarr=['red','blue'];

Need %hash value for the following

UPDATE:: Sorry, I need Hash key conains two array

$hash = { 'first' => [ 'rose', 'orange', 'green' ], [ 'blue', 'red' ] };

Please guide me

Thanks
Rose

Replies are listed 'Best First'.
Re: push an array into Hash of Array
by wfsp (Abbot) on Sep 16, 2008 at 14:57 UTC
    Your update shows you need a HoAoA.
    #!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; my @array1 = qw{rose orange green}; my @array2 = qw{blue red}; my %hash; push @{$hash{q{first}}}, \@array1, \@array2; print Dumper \%hash;
    $VAR1 = { 'first' => [ [ 'rose', 'orange', 'green' ], [ 'blue', 'red' ] ] };
    update: clarified
      Is q{first} == 'first' ? If so, I wonder why you would use the former in a line of code with so many curly braces. Just curious.
        Is q{first} == 'first'
        Yes, see perlop.

        Why use it? I find it easier to parse than ' and have adopted braces as delimiters and tend to stick to them. ymmv.

        In this case though $hash{first} works find (no need for either).

Re: push an array into Hash of Array
by kyle (Abbot) on Sep 16, 2008 at 14:28 UTC

    This is actually creating an array with one element, which is an array reference:

    my @arr=['rose','orange','green'];

    What you actually want is to use parentheses, as I do below.

    use Data::Dumper; my @arr = ( 'rose', 'orange', 'green' ); my %hash; $hash{'first'} = \@arr; print Dumper \%hash; my @newarr = ( 'red', 'blue' ); push @{ $hash{'first' } }, @newarr; print Dumper \%hash; __END__ $VAR1 = { 'first' => [ 'rose', 'orange', 'green' ] }; $VAR1 = { 'first' => [ 'rose', 'orange', 'green', 'red', 'blue' ] };

    You might want to look at perldata, perlreftut, perlref, and References quick reference.

Re: push an array into Hash of Array
by pjotrik (Friar) on Sep 16, 2008 at 14:29 UTC
    Are you sure you want that double referencing of the array? Instead of
    my @arr=['rose','orange','green']; $hash{"first"}=[@arr];
    it makes more sense to have
    my @arr = qw(rose orange green); $hash{"first"}=\@arr;
    To add to this array, simply
    push(@{$hash{first}}, qw(red blue));
    In entirety:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %hash; my @arr = qw(rose orange green); $hash{"first"}=\@arr; push(@{$hash{first}}, qw(red blue)); print Dumper(\%hash); ----- $VAR1 = { 'first' => [ 'rose', 'orange', 'green', 'red', 'blue' ] };
    Update: for your updated question
    $hash{first} = []; push(@{$hash{first}}, \@arr); ... @newarr = qw(red blue); push(@{$hash{first}}, \@newarr);