Re: Joining/Merging AoA
- From: Darin McBride <dmcbride@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 22 Apr 2008 15:18:45 GMT
Vishal G wrote:
Hi Guys,
I have a little complicated problem...
I have two arrays
@a = ( ['id', 'name', 'age'],
['1', 'Fred', '24'],
['2', 'Frank', '42'],
);
@b = ( ['id', 'sex'],
['1', 'm' ],
['2', 'm'],
);
I want to join these two AoA, based on id, so the resulting array will
look like this
"Based on id" sounds like @a and @b won't always be in the same order...
@c = ( ['id', 'name', 'age', 'sex'],
['1', 'Fred', '24', 'm' ],
['2', 'Frank', '42', 'm'],
);
Any Ideas?
# get everything in a map based on the first field...
my %b = map { $_->[0] => [ @$_[1..$#$_] ] } @b;
# merge ...
@a = map { [ @$_, @{$b{$_->[0]}} ] } @a;
That should be clear.... right? ;-)
.
- References:
- Joining/Merging AoA
- From: Vishal G
- Joining/Merging AoA
- Prev by Date: Joining/Merging AoA
- Next by Date: ANNOUNCE: Text-CSV_XS 0.43
- Previous by thread: Joining/Merging AoA
- Next by thread: ANNOUNCE: Text-CSV_XS 0.43
- Index(es):