Scripting question

Jonathan McKeown jonathan+freebsd-questions at hst.org.za
Fri Sep 14 01:35:19 PDT 2007


On Friday 14 September 2007 09:42, Steve Bertrand wrote:
> >>> I don't have the perl skills, though that would be ideal.
>
> -- snip --
>
> > Another approach in Perl would be:
> >
> > #!/usr/bin/perl
> > my (%names, %dups);
> > while (<>) {
> >     my ($key) = split;
> >     $dups{$key} = 1 if $names{$key};
> >     $names{$key} = 1;
> > }
> > delete @names{keys %dups};

> I don't know if this is completely relevant, but it appears as though it
>  may help.
>
> Bob Showalter once advised me on the Perl Beginners list as such,
> quoted, but snipped for clarity:
>
> see "perldoc -q duplicate" If the array elements can
> be compared with string semantics (as you are doing here), the following
> will work:
>
>    my @array = do { my %seen; grep !$seen{$_}++, @clean };

The problem with this is that it leaves you with one copy of each duplicated 
item: the requirement was to remove all copies of duplicated items and return 
only the non-repeated items.

Jonathan


More information about the freebsd-questions mailing list