http://qs1969.pair.com?node_id=61602


in reply to Detecting duplicate entries

The hash solutions that have already been given have the right idea, but you can use hash slices to make it look a bit simpler.

my %uniq; @uniq{@orig_list} = @orig_list; my @unique_list = keys %uniq;
--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re: Re: Detecting duplicate entries
by japhy (Canon) on Mar 01, 2001 at 22:24 UTC
    Don't assign values, just assign ().

    Or use:

    $seen{$_}++ or push @uniq, $_ for @orig; # or @uniq = grep !$seen{$_}++, @orig;


    japhy -- Perl and Regex Hacker
Re: Re: Detecting duplicate entries
by petral (Curate) on Mar 01, 2001 at 23:48 UTC
    And probably even slower, but it avoids the %temp and it's cute:
    sort keys %{ { map { $_ => 1 } @orig_list }

    p
        Ok, but it keeps up with sort -u above.

        p
Re: Re: Detecting duplicate entries
by merlyn (Sage) on Mar 01, 2001 at 22:11 UTC