Joining/Merging AoA

Hi Guys,

I have a little complicated problem...

I have two arrays

[at] a = ( ['id', 'name', 'age'],
['1', 'Fred', '24'],
['2', 'Frank', '42'],
);

[at] 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

[at] c = ( ['id', 'name', 'age', 'sex'],
['1', 'Fred', '24', 'm' ],
['2', 'Frank', '42', 'm'],
);

Any Ideas?

Thanks in advance.
Vishal G [ Di, 22 April 2008 08:35 ] [ ID #1948052 ]

Re: Joining/Merging AoA

Vishal G wrote:

> Hi Guys,
>
> I have a little complicated problem...
>
> I have two arrays
>
> [at] a = ( ['id', 'name', 'age'],
> ['1', 'Fred', '24'],
> ['2', 'Frank', '42'],
> );
>
> [at] 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 [at] a and [at] b won't always be in the same order...

> [at] 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] => [ [at] $_[1..$#$_] ] } [at] b;
# merge ...
[at] a = map { [ [at] $_, [at] {$b{$_->[0]}} ] } [at] a;

That should be clear.... right? ;-)
Darin McBride [ Di, 22 April 2008 17:18 ] [ ID #1948053 ]
Perl » comp.lang.perl.modules » Joining/Merging AoA

Vorheriges Thema: ANNOUNCE: Text-CSV_XS 0.43
Nächstes Thema: comp.lang.perl.modules charter