in reply to combine duplicate entries

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.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Replies are listed 'Best First'.
Re^2: combine duplicate entries
by Anonymous Monk on Aug 24, 2006 at 07:31 UTC
    Excellent job! It works perfectly!
    Thank you very much!