[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Merging elements



On Thu, Oct 12, 2000 at 04:59:00PM -0500, Pete Prodoehl wrote:
> 
> I'm a bit stuck, and would appreciate any help. I've tried doing this with a
> hash containing arrays, two separate arrays, and some other methods that
> were even more ineffective. My head is starting to hurt... There's probably
> some simple way to do this, I just can't figure it out...
> 
> I'm reading in a file with data like this:
> 
>   aaa  438287,3231
  bbb  539634,4342
  ccc  558180,43555
  aaa  10878,8678666
  bbb  9274,6546
  ccc  23386,393
> 
> where 'aaa' is a key appearing twice, and the two separate pieces of data
> (438287,3231 and 10878,8678666) need to be combined into one single piece of
> data, represented by aaa, so I'm trying to get the outcome to be:
> 
>   aaa  438287,3231,10878,8678666
>   bbb  539634,4342,9274,6546
>   ccc  558180,43555,23386,393
> 
> any help or pointers would be  appreciated...

Would a simple hash be sufficient?

while (<>) {
  chomp;
  ($key, $data) = split;
  $data{$key} .= ',' if defined $data{$key};
  $data{$key} .= $data;
}

foreach $key (sort keys %data) {
  print "  $key  $data{$key}\n";
}


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org