Hi there Monks!
I am trying to merge these two arrays only if the acc matches, can any one tell me what could be the best way to do this?
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $data1 = [
{
'NAME' => '1PAUL DY',
'DATE' => '12009-05-05',
'NUMBER' => '100001',
'ACC' => '1A1A',
},
{
'NAME' => '2PAUL DY',
'DATE' => '2011-01-05',
'NUMBER' => '200331',
'ACC' => '2A3B',
},
{
'NAME' => '4PAUL DY',
'DATE' => '42011-01-05',
'NUMBER' => '4200331',
'ACC' => '6A4B',
},
];
my $data2 = [
{
'EXT1' => '1b',
'EXT2' => '12b',
'EXT3' => '13b',
'EXT4' => '14d',
'ACC' => '1A1A',
},
{
'EXT1' => '2b',
'EXT2' => '2b',
'EXT3' => '2b',
'EXT4' => '2d',
'ACC' => '2A3B',
},
{
'EXT1X' => '4b',
'EXT2X' => '4b',
'EXT3X' => '4b',
'EXT4X' => '4d',
'ACC' => '4A4B',
},
];
# Merge all the data
my @all_data;
foreach my $one_data ( @{ $data1 } ) {
my $sec_data = shift @{ $data2};
push @all_data, { %{ $one_data }, %{ $sec_data } };
}
print Dumper \@all_data;
Thanks for the help!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.