This method will allow records to appear in any order, meaning that "Peter white" and "Peter brown" don't necessarily have to appear grouped together in the original data set. That's the pro to the following solution. The con is that there is no effort made to preserve the original order (just as no assumptions are made that the original set came in any particular order in the first place). It would be trivial to sort alphabetically though, or even to keep track of original order if necessary.
A hash of arrays is used, and color values are pushed into arrays keyed off of the name field. Here's the code:
use strict; use warnings; my %sets; while( <DATA> ) { chomp; next unless length; my( $entry, $color ) = split /\s+/; push @{$sets{$entry}}, $color; } foreach my $entry ( keys %sets ) { print $entry; foreach my $color ( @{$sets{$entry}} ) { print "\t$color\n"; } } __DATA__ John red Mike yellow Peter white Peter brown Jim purple Antony orange Antony green George black
Dave
In reply to Re: combine duplicate entries
by davido
in thread combine duplicate entries
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |