in reply to finding unique elements in an array

I recently used this example from the Cookbook which worked very well for me...
(There are other methods in the book so it's a highly recommended resource):
%seen = (); @uniq = (); foreach $item (@list) { unless ($seen{$item}) { # if we get here, we have not seen it before $seen{$item} = 1; push(@uniq, $item); } }

Update: Inserted missed bracket spotted by Martymart

Replies are listed 'Best First'.
Re: Re: finding unique elements in an array
by Anonymous Monk on May 19, 2003 at 09:07 UTC
    I tried two of these methods and they both worked for me... The method from zby was short and elegant looking, however the one from the cookbook gave me a result that more closely matched the input for some reason. Spotted a minor typo in that code though. Change line:unless ($seen{$item})to
    unless ($seen{$item}){

    Good answers to the question though,
    Martymart