Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

combine duplicate entries

by Anonymous Monk
on Aug 24, 2006 at 06:54 UTC ( #569303=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!
I am confused with the following problem:
I have several lines, like the followings:
John red Mike yellow Peter white Peter brown Jim purple Antony orange Antony green George black
My question is, how can I print:
John red Mike yellow Peter white brown Jim purple Antony orange green George black
Any hints?
Thanks in advance!

Comment on combine duplicate entries
Select or Download Code
Re: combine duplicate entries
by Samy_rio (Vicar) on Aug 24, 2006 at 07:06 UTC

    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';

      Excellent job! It works perfectly!
      Thank you very much!
Re: combine duplicate entries
by davido (Archbishop) on Aug 24, 2006 at 08:04 UTC

    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

Re: combine duplicate entries
by Mandrake (Chaplain) on Aug 24, 2006 at 09:19 UTC
    Hash of array refs. Reads and print irrespective of the information is collective or not.
    #!/usr/bin/perl -w use strict; my %hash; (!/^$/) && (push @{$hash{(split /\s+/,$_)[0]}}, (split /\s+/,$_)[1]) w +hile(<DATA>); for my $key (keys %hash) { (print "$key\t$_\n" ) && ($key="") for (@{$hash{$key}}) ; } __DATA__ John red Mike yellow Peter white Peter brown Jim purple Antony orange Antony green George black
Re: combine duplicate entries
by BigJoe (Curate) on Aug 24, 2006 at 15:23 UTC
    Sounds like a homework assignment to me. Is school starting already?


    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://569303]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-09-16 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (158 votes), past polls