Hi, This will help you only if duplicate lines are collectively.
use strict; use warnings; my $pre = ''; while (<DATA>) { chomp($_); next if /^$/; my ($name, $color) = split /\s+/, $_; ($name ne $pre) ? (print "$name\t$color\n") : (print "\t$color\n") +; $pre = $name; } __DATA__ John red Mike yellow Peter white Peter brown Jim purple Antony orange Antony green George black Output: ------- John red Mike yellow Peter white brown Jim purple Antony orange green George black
Updated : TIMTOWTDI
If duplicate lines are not in collective then use this,
use strict; use warnings; use Tie::IxHash; my %hash; tie %hash, "Tie::IxHash"; while (<DATA>) { chomp($_); next if /^$/; my ($name, $color) = split /\s+/, $_; ($hash{$name}) ? ($hash{$name}.="\n\t$color") : ($hash{$name}="$co +lor"); } print "$_\t$hash{$_}\n" for keys (%hash); __DATA__ John red Mike yellow Peter white Jim purple Peter brown Antony orange George black Antony green Output: ------- John red Mike yellow Peter white brown Jim purple Antony orange green George black
Regards,
Velusamy R.
In reply to Re: combine duplicate entries
by Samy_rio
in thread combine duplicate entries
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |