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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Removing duplicate array elements
by Chief of Chaos (Friar) on Feb 16, 2003 at 20:37 UTC
    Hi kumar, maybe this can help you :
    #/usr/bin/perl -w use strict; my @array1 = ('one','two','one','three','four','two','one'); my @array2 = (); my $index; my $in; foreach $in (@array1) { push(@array2,$in) if (!grep $array2[$_] eq $in, 0 .. $#array2); } print "@array1\n"; print "@array2\n"; __DATA__ # the result : one two one three four two one one two three four
    This is not the efficent dup-finder. But it is a "way".
    C-o-C
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how to remove
by artist (Parson) on Feb 16, 2003 at 20:49 UTC
    @test = grep { $X{$_}} map { defined $X{$_} or ($X{$_} += 1),$_ } @test;
      Not that I'm all into golf, but...
      @test = do { my %s; grep { !$s{$_}++ } @test };
      That's assuming you want the values in the order in which they first occur.
      If that's not necessary, then you can simply use a hash as a set:
      @test = do { my %s; @s{@test} = @test; values %test };

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.

        20 strokes.....

        sub unique { #2345678901234567890 $_{$_}++for@_;keys%_ } @unique = unique( qw( 1 2 3 4 5 1 3 5 1 3 5 ) ); print "@unique"; __DATA__ 1 2 3 4 5

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks... Excellent Performance... I am new to this site. How can i post questions to this site? Can i award points to the person who helps to solve problem. Could you pls explain your written code? Thanks a lot.
Re: Removing duplicate array elements
by jasonk (Parson) on Feb 16, 2003 at 19:43 UTC

    This is covered in the FAQ perlfaq.

      I am not sure why, but your link did not work for me - I found www.perlfaq.com helpful, kumar's exact question can be found here.

      I also could not find a solution in the perldoc.com FAQ specifically addressing this question (though I am sure it is there somewhere).

      I however did find kumar's question (and answer) here on PM in our own Q/A section here.

      Cheers - L~R

      Update: Links point directly to solution