Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Merging/Joining AoA

Reply
Thread Tools

Merging/Joining AoA

 
 
Vishal G
Guest
Posts: n/a
 
      04-22-2008
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.
 
Reply With Quote
 
 
 
 
Ben Bullock
Guest
Posts: n/a
 
      04-22-2008
On Apr 22, 3:35 pm, Vishal G <(E-Mail Removed)> 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?


my %akeys;
@akeys{map $$_[0],@a} = @a;
my %bkeys;
@bkeys{map $$_[0],@b} = @b;
my @c;
for (sort keys %akeys) {
if ($bkeys{$_}) {
push @c, [@{$akeys{$_}},@{$bkeys{$_}}[1..$#{$bkeys{$_}}]];
}
}
for (@c) {
print "[",join (", ",@$_),"]\n";
}
 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      04-22-2008
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:umper;
print Dumper \@a;
 
Reply With Quote
 
Hartmut Camphausen
Guest
Posts: n/a
 
      04-22-2008
In <<(E-Mail Removed)>>
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{$_} ? @{$a{$_}} : ($_, ('') x $#afields),
$b{$_} ? @{$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
 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      04-22-2008
On Apr 22, 2:35*am, Vishal G <(E-Mail Removed)> 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?


I'm not sure how much control you have over the structure of the data,
but it seems to me that you would be better served if the data were
stored in hashes to begin with:

use strict; use warnings;
use Data:umper;

my %people = (
1 => { name => 'Fred', age => 24 },
2 => { name => 'Frank', age => 42 },
);

my %gender = (
1 => { sex => 'm' },
2 => { sex => 'm' },
);

...then it's a simple matter to combine the two structures, keyed by
the id.

for my $id ( keys %people ) {
$people{$id}{sex} = $gender{$id}{sex};
}



print Dumper( \%people ), "\n";


 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      04-22-2008
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.

 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      04-22-2008
On Apr 22, 11:11*am, nolo contendere <(E-Mail Removed)> wrote:
> On Apr 22, 2:35*am, Vishal G <(E-Mail Removed)> 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?

>
> I'm not sure how much control you have over the structure of the data,
> but it seems to me that you would be better served if the data were
> stored in hashes to begin with:
>
> use strict; use warnings;
> use Data:umper;
>
> my %people = (
> * 1 => { name => 'Fred', *age => 24 },
> * 2 => { name => 'Frank', age => 42 },
> );
>
> my %gender = (
> * 1 => { sex => 'm' },
> * 2 => { sex => 'm' },
> );
>
> ...then it's a simple matter to combine the two structures, keyed by
> the id.
>
> for my $id ( keys %people ) {
> * * $people{$id}{sex} = $gender{$id}{sex};
>
> }
>
> print Dumper( \%people ), "\n";


One more thing, are you certain that you wanted to quote the age
values in your data? in most cases perl is smart enough to dwym, but
for clarity i think it would be better to leave them as numbers rather
than strings.
 
Reply With Quote
 
benkasminbullock@gmail.com
Guest
Posts: n/a
 
      04-23-2008
nolo contendere <(E-Mail Removed)> wrote:

> I'm not sure how much control you have over the structure of the data,
> but it seems to me that you would be better served if the data were
> stored in hashes to begin with:


If the person needs to know how to put in a screw, why tell him he
would be better served by using nails and a hammer?

A quote from Stephen King (viewable at
http://www.nsftools.com/blog/blog-03-2006.htm) comes to mind:

"I want to suggest to you that to write to the best of your abilities,
it behooves you to construct your own toolbox and then build up enough
muscle so you can carry it with you. Then, instead of looking at a
hard job and getting discouraged, you will perhaps seize the correct
tool and get immediately to work."

In the real world, there are lots of situations in which you're likely
to get data in an array of arrays. For example if you take data from a
"Range" in an Excel spreadsheet, you'll get it back as an array of
arrays, just like the original poster's problem. Instead of looking at
a hard job and getting discouraged, I'd suggest you think about
seizing the correct tool and getting immediately to work.
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      04-23-2008
On Tue, 22 Apr 2008 21:55:03 -0700 Keith Keller <(E-Mail Removed)-francisco.ca.us> wrote:

KK> use Shining;

KK> my $shine=Shining->new;
KK> $shine->enterRoom(237);
KK> $shine->avoid('Dad');
KK> $shine->call('cook');
KK> $shine->REDRUM;
KK> $shine->escape('hedgeMaze','snowmobile');

I think you forgot to include the POD:

All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      04-23-2008
On Apr 22, 11:49*pm, (E-Mail Removed) wrote:
> nolo contendere <(E-Mail Removed)> wrote:
> > I'm not sure how much control you have over the structure of the data,
> > but it seems to me that you would be better served if the data were
> > stored in hashes to begin with:

>
> If the person needs to know how to put in a screw, why tell him he
> would be better served by using nails and a hammer?



huh? i began my suggestion with the caveat that this would be relevant
if the OP had the control to choose the data structures in which he
housed his data. to follow up on your analogy, if the person needs to
know how to build a house, and is scratching his head wondering how to
do it with only screws and a screwdriver, yes, i could help him by
telling him how to go about it.

or, i can point out that there is a tool shed around the corner of
which he is unaware, and then proceed to pick out more appropriate
tools that he can use to better build his house.

here's another analogy. if he asked, how do i add the following?
<<END_WORD_PROBLEM;

i want to add three 6's and six 4's using addition. sure, you could
say: well, first compute the sum of the three 6's, like so--
6 + 6 + 6 = 18.
then, compute the sum of six 4's, like so--
4 + 4 + 4 + 4 + 4 + 4 = 24.
finally, add the two sums together.
18 + 24 = 42.
END_WORD_PROBLEM

Or, I could make life a whole lot easier by suggesting:
just do: (3 x 6) + (6 x 4) = 42.

what's wrong with that? at the very worst, if there are restrictions
in place that prohibit him from using multiplication for this
particular problem, he is at least aware that there is a better
solution for similar problems in the future, when such restrictions
may may be lifted, or not even exist at all.

...or did you just want an excuse to post that quote by Steven
King? .

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Eval in Constructing AoA Gundala Viswanath Perl Misc 2 09-04-2008 09:48 PM
construct AoA with REXML XPath? Chris McMahon Ruby 3 03-16-2006 04:50 AM



Advertisments