Click here to get back home

Merging/Joining AoA

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Merging/Joining AoA Vishal G 04-22-2008
Posted by Vishal G on April 22, 2008, 2:35 am
Please log in for more thread options
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?

Thanks in advance.

Posted by Ben Bullock on April 22, 2008, 3:13 am
Please log in for more thread options
> 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?

my %akeys;
@akeys{map $$_[0],@a} = @a;
my %bkeys;
@bkeys{map $$_[0],@b} = @b;
my @c;
for (sort keys %akeys) {
if ($bkeys) {
        push @c, [@},@}[1..$#}]];
}
}
for (@c) {
print "[",join (", ",@$_),"]\n";
}

Posted by Hartmut Camphausen on April 22, 2008, 10:24 am
Please log in for more thread options
schrieb Ben Bullock...

> [...]
> my @c;
> for (sort keys %akeys) {
> [...]
> }
> for (@c) {
> print "[",join (", ",@$_),"]\n";
> }

Prints [1, Fred, 24, m]
[2, Frank, 42, m]
[id, name, age, sex]

Take care of the 'sort' ;-)


Alternatively, if you want to take care of the IDs of /both/ arrays,
you could write:


#!perl.exe -w # well, on Windows...
use strict; # always 'use' this one!

my @a = ( ['id', 'name', 'age' ],        # just our data...
['2', 'Frank', '42' ],
['1', 'Fred', '24' ],
['3', 'Pat', '36' ],
);
my @b = ( ['id', 'sex' ], # ...to deal with
['1', 'm' ],
['2', 'm' ],
['4', '??' ],
);

my @afields = @{ shift @a }; # get rid of field names,
my @bfields = @{ shift @b }; shift @bfields; # keeping them at hand
my %a = map {$_->[0], $_} @a; # now we can refer to the...
my %b = map {shift @, $_} @b; # ...data by their IDs

my %seen; # just to avoid duplicates
my @c = ( [ @afields, @bfields ], # field names come first
map { [ $a ? @} : ($_, ('') x $#afields),
$b ? @} : ( ('') x @bfields )
]
} sort grep { $seen++ ? 0 : $_ }
keys (%a), keys (%b)
);


...gives

['id', 'name', 'age', 'sex']
['1', 'Fred', '24', 'm' ]
['2', 'Frank', '42', 'm' ]
['3', 'Pat', '36', '' ]
['4', '', '', '??' ]



mfg, Hartmut


--
------------------------------------------------
Hartmut Camphausen h.camp[bei]textix[punkt]de

Posted by Ben Bullock on April 22, 2008, 11:30 am
Please log in for more thread options
On Tue, 22 Apr 2008 16:24:19 +0200, Hartmut Camphausen wrote:

> Prints [1, Fred, 24, m]
> [2, Frank, 42, m]
> [id, name, age, sex]

Yes, I know. So what?

> Take care of the 'sort' ;-)

I don't care about the order of these results. The original poster can
figure out all the details if he wants to.

> Alternatively, if you want to take care of the IDs of /both/ arrays, you
> could write:

Yes, but I have no way to really guess in detail what the original poster
wanted, so why spend a lot of time trying to cover every possibility? I
wrote that initial answer in only a minute or two, and I think it's good
enough to get the poster on track towards an answer.


Posted by Matija Papec on April 22, 2008, 8:26 am
Please log in for more thread options
Vishal G wrote:
> 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'],
> );

Assuming that both @a and @b have same size and same id order,

for my $i (0 .. $#a) {
my $at = $a[$i];
my $bt = $b[$i];
push @$at, @$bt[1 .. $#$bt];
}
use Data::Dumper;
print Dumper \@a;


Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap