in reply to How I learned to stop worrying and love the hash slice. (boo)
in thread beyond me

I frequently use hash slices to eliminate duplicates from an array.

A simplified example:

my @a = (1,2,3,2,1); my %a; @a{@a} = (1) x @a; my @unique_a = keys %a;
Of course, this does not preserve the order of the elements as they were in original array @a.

Replies are listed 'Best First'.
Re: Re: How I learned to stop worrying and love the hash slice. (boo)
by blakem (Monsignor) on Aug 14, 2001 at 14:24 UTC
    Perhaps grep might work better here?
    my @a = (1,2,3,2,1); my %seen; my @unique_a = grep {!$seen{$_}++} @a; print "A: $_\n" for (@a); print "Unique: $_\n" for (@unique_a);
    Assuming you wanted to keep the first occurance, this will preserve the original order.

    Or you can use map to create your %a hash.

    my @a = (1,2,3,2,1); my %a = map {$_=>1} @a; my @unique_a = keys %a; print "A: $_\n" for (@a); print "Unique: $_\n" for (@unique_a);

    -Blake