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

Based on the 3 conditions I need to generate 3 arrays of hashes ( long data from DB) and join them into 1, maintaining the same format. The format I get from the DB is this:
[ {fruit=>apple, vegetable=> pumpkin, }, {fruit=>mango, vegetable=> tomatoes, }, {fruit=>strawberry, vegetable=> pumpkin, }, ]
So all potential hashes will be generated in this format. I tried the following:
my $arrayHash1 =(); my $arrayHash2 =(); my $arrayHash3 =(); my $FinalMegaHash =(); if ( condition1==true){ generate arrayHash1; $FinalMegaHash .= $arrayHash1; } if ( condition2==true){ generate arrayHash2; $FinalMegaHash .= $arrayHash2; } if ( condition3==true){ generate arrayHash3; $FinalMegaHash .= $arrayHash3; } print Dumper $FinalMegaHash;
I know the syntax is wrong, but it illustrates the idea of what I want to do. Have been googling all over the place, can't find anything. :/ Any guidance would be much appreciated. Thank you

Replies are listed 'Best First'.
Re: combining 3 arrays of hashes
by johngg (Canon) on Aug 19, 2016 at 19:57 UTC

    Firstly, use the @ sigil for arrays. Secondly, string concatenation is not appropriate for arrays, use push instead. Here is a simplified solution.

    johngg@shiraz:~/perl/Monks > perl -MData::Dumper -Mstrict -Mwarnings - +E ' my @AoH1 = ( { fruit => q{apple}, veg => q{pumpkin} }, { fruit => q{pear}, veg => q{swede} }, { fruit => q{cherry}, veg => q{pea} } ); my @AoH2 = ( { fruit => q{mango}, veg => q{cabbage} }, { fruit => q{plum}, veg => q{leek} }, ); my @megaAoH = (); push @megaAoH, @AoH1 if 1; push @megaAoH, @AoH2 if 1; print Data::Dumper->Dumpxs( [ \ @megaAoH ], [ qw{ *megaAoH } ] );' @megaAoH = ( { 'fruit' => 'apple', 'veg' => 'pumpkin' }, { 'veg' => 'swede', 'fruit' => 'pear' }, { 'fruit' => 'cherry', 'veg' => 'pea' }, { 'veg' => 'cabbage', 'fruit' => 'mango' }, { 'fruit' => 'plum', 'veg' => 'leek' } ); johngg@shiraz:~/perl/Monks >

    I hope this is helpful.

    Update: If you are actually using array references rather than arrays then square brackets should be used instead of parentheses and de-reference them for the push operation.

    johngg@shiraz:~/perl/Monks > perl -MData::Dumper -Mstrict -Mwarnings - +E ' my $refToAoH1 = [ { fruit => q{apple}, veg => q{pumpkin} }, { fruit => q{pear}, veg => q{swede} }, { fruit => q{cherry}, veg => q{pea} } ]; my $refToAoH2 = [ { fruit => q{mango}, veg => q{cabbage} }, { fruit => q{plum}, veg => q{leek} }, ]; my $refTomegaAoH = []; push @{ $refTomegaAoH }, @{ $refToAoH1 } if 1; push @{ $refTomegaAoH }, @{ $refToAoH2 } if 1; print Data::Dumper->Dumpxs( [ $refTomegaAoH ], [ qw{ refTomegaAoH } ] +);' $refTomegaAoH = [ { 'fruit' => 'apple', 'veg' => 'pumpkin' }, { 'fruit' => 'pear', 'veg' => 'swede' }, { 'fruit' => 'cherry', 'veg' => 'pea' }, { 'fruit' => 'mango', 'veg' => 'cabbage' }, { 'veg' => 'leek', 'fruit' => 'plum' } ]; johngg@shiraz:~/perl/Monks >

    Cheers,

    JohnGG

      JohnGG - You are the best! The second solution for the array references worked like a charm. Thank you SO much! Have a wonderful weekend