On Tue, Apr 22, 2008 at 2:28 AM, Vishal G <[EMAIL PROTECTED]> 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
>
>  @c = ( ['id', 'name', 'age', 'sex'],
>            ['1', 'Fred', '24', 'm' ],
>            ['2', 'Frank', '42', 'm'],
>          );
>
>  Any Ideas?
>

It really depends on the data. Are you certain that both lists will be
in the same order? Are you certain that all data will exist for each
person? If so you can just loop though both lists. That's not very
robust, though, because as soon as you get a value that is one list
and not the other, everything gets out of sync.

I'd probably do something like the following, which stores the is in
the array index, as well as the first element:


    push my @c, [EMAIL PROTECTED] @a}];
    push @{$c[0]}, (@{shift @b})[1];

    $c[$_->[0]] = [ @{$_} ] while $_ = shift @a;
    push @{$c[$_->[0]]}, $_->[1] while $_ = shift @b;


HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!

Reply via email to