in reply to compile errors

#! /usr/bin/env perl package Foo; use strict; use warnings; sub new { bless {}, shift } sub uniq { shift; my %h; $h{$_}++ for @_; grep { $h{$_} == 1 } keys %h + } package main; print "@{[Foo->new->uniq(@ARGV)]}\n";
...
./11911 hi hi hi hey lo hey may ':-)' may lo :-)

Replies are listed 'Best First'.
Re: compile errors
by jonadab (Parson) on Feb 25, 2006 at 14:16 UTC

    If I am not mistaken, that removes even the first instance of any entry that has a duplicate. Usually one wants to remove only subsequent entries, leaving the first. For that, cache the count on the fly:

    sub uniq { my %cache; return grep { not $cache{$_}++ } @_ }
    print join " ", uniq qw(foo bar baz foo quux foo wibble bar quux wibble bongo foo dazzle);
    Results:
    foo bar baz quux wibble bongo dazzle