in reply to Re: how to remove
in thread Removing duplicate array elements

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.

Replies are listed 'Best First'.
Re: Re: Re:(GOLF) Removing duplicate array elements
by tachyon (Chancellor) on Feb 16, 2003 at 23:44 UTC

    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.

        18...I knew there was a shorter way

        $a = [1,2,3]; $b = [3,4,5]; $c = [6,7,8]; @test = unique($a,$b,$c,$a,$b); use Data::Dumper; print Dumper \@test; sub unique { #23456789012345678 @_{@_}=@_;values%_ }

        cheers

        tachyon

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