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

Dear Perl Experts,

I have a list of names followed by their page numbers

e.g.
Chee S. L. 8 Cheng T. H. 5 Cheng T. H. 2 Chetan M. 4

I want to remove the duplicate occurance of the names and arrange the numbers in the following way

required ouput:

Chee S. L. 8 Cheng T. H. 2,5 Chetan M. 4

Please advice me how to achieve this.

Thanks in advance

janitored by ybiC: Renamed from "sorting", because one-word nodetitles hinder site search

Replies are listed 'Best First'.
Re: Sorting a list and removing duplicate elements
by hardburn (Abbot) on Aug 17, 2004 at 16:34 UTC

    Whenever you think about "remove duplicates" in Perl, think hashes. In this case, you probably want to have each hash entry be an array containing the numbers:

    my %data; while(<>) { chomp; my ($name, $number) = $_ =~ /\A (\D+) (\d+) \z/x; push @{ $data{$name} }, $number if $name && $number; }

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Sorting a list and removing duplicate elements
by davorg (Chancellor) on Aug 17, 2004 at 16:40 UTC
    #!/usr/bin/perl use strict; use warnings; my %data; while (<DATA>) { chomp; next unless /\S/; my ($name, $num) = /(.*)\s+(\d+)/; push @{$data{$name}}, $num if ($name && $num); } foreach my $name (sort keys %data) { print "$name "; print join ',', sort { $a <=> $b } @{$data{$name}}; print "\n\n"; } __END__ Chee S. L. 8 Cheng T. H. 5 Cheng T. H. 2 Chetan M. 4
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      wonderful, your code does the magic

      thank you very much

      Thank you for your help, but one more difficulty i have

      how to remove the duplicate numbers, e.g.

      Cheng T. H. 2,2,5
      Chetan M. 4

      this one should be as

      Cheng T. H. 2,5
      Chetan M. 4

      i.e. here i want to remove 2 from first entry

      Thanks in advance

        Change ${data}{$name} to be a hash instead of an array.

        #!/usr/bin/perl use strict; use warnings; my %data; while (<DATA>) { chomp; next unless /\S/; my ($name, $num) = /(.*)\s+(\d+)/; $data{$name}{$num} = 1; } foreach my $name (sort keys %data) { print "$name "; print join ',', sort { $a <=> $b } keys %{$data{$name}}; print "\n\n"; } __END__ Chee S. L. 8 Cheng T. H. 5 Cheng T. H. 2 Chetan M. 4 Cheng T. H. 2
        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Sorting a list and removing duplicate elements
by pbeckingham (Parson) on Aug 17, 2004 at 16:43 UTC

    Use a hash.

    #! /usr/bin/perl -w use strict; my %data; while (<DATA>) { my ($name, $page) = /^(.*?)\s+(\d+)$/; push @{$data{$name}}, $page; } print $_, ' ', join (',', sort @{$data{$_}}), "\n" for sort keys %da +ta; __DATA__ Chee S. L. 8 Cheng T. H. 5 Cheng T. H. 2 Chetan M. 4 __OUTPUT__ Chee S. L. 8 Cheng T. H. 5,2 Chetan M. 4



    pbeckingham - typist, perishable vertebrate.