in reply to Removing duplicate array elements

@test = grep { $X{$_}} map { defined $X{$_} or ($X{$_} += 1),$_ } @test;

Replies are listed 'Best First'.
Re: Re: Removing duplicate array elements
by jdporter (Paladin) on Feb 16, 2003 at 21:07 UTC
    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

        Doesn't work for things that don't survive a round trip through stringification, such as references.

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

Re: Re: how to remove
by kumar (Initiate) on Feb 17, 2003 at 16:32 UTC
    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.