Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: combine duplicate entries

by Samy_rio (Vicar)
on Aug 24, 2006 at 07:06 UTC ( [id://569306]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://569306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found