Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am having a text file which consist of details splitted by tabspace. I want to remove the duplicates in the data.

Input: 1 101 2 102 3 103 1 104 2 102 3 104 output: 1 101 104 2 102 3 103 104

Many thanks in advance.

cheers.

Replies are listed 'Best First'.
Re: Removing duplicates
by edan (Curate) on Jul 05, 2004 at 08:44 UTC
    #!/perl -l use strict; use warnings; my %merge; while ( <DATA> ) { chomp; my ($key, $value) = split /\t/; $merge{$key}{$value} = $value; } for my $key ( keys %merge ) { my @values = keys %{$merge{$key}}; print "$key => @values"; } __DATA__ 1 101 2 102 3 103 1 104 2 102 3 104 __output__ 1 => 104 101 3 => 104 103 2 => 102

    Update: First draft was wrong, misread of the question. Updated within 2 minutes. Apologies if anyone read the first version. :)

    --
    edan

Re: Removing duplicates
by borisz (Canon) on Jul 05, 2004 at 09:02 UTC
    This peace of code does exactly what you want.
    #!/usr/bin/perl my %t; while ( defined($_ = <DATA>) ){ chomp; my ($k, $v) = split /\s+/; push @{$t{$k}}, $v; } for ( sort keys %t ) { my %q; @q{@{$t{$_}}} = (); print "$_ ", join( ' ', sort keys %q), "\n"; } __DATA__ 1 101 2 102 3 103 1 104 2 102 3 104
    Boris
Re: Removing duplicates
by davido (Cardinal) on Jul 05, 2004 at 08:40 UTC
    my %unique; while ( my $line = <DATA> ) { chomp $line; my ( $left, $right ) = split /\s+/, $line; push @{$unique{$left}}, $right; } foreach my $key ( sort keys %unique ) { print "$_\t"; foreach my $item ( @{$unique{$key}} ) { print "$item "; } print "\n"; }

    Dave

      You made the same mistake that I did at first. The OP wants to not only merge the values by keys, but also remove duplicate values for each key...

      --
      edan