in reply to combining 3 arrays of hashes
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: combining 3 arrays of hashes
by pearlgirl (Novice) on Aug 19, 2016 at 21:21 UTC |