in reply to Re: List processing performance
in thread List processing performance

playing golf,
my @words = ( ... ); my @common = qw|a and at|; my %seen; @seen{@common} = (1) x @common; @words = grep { $_ = lc; ! $seen{$_}++ } sort @words;
:-) Autark

Replies are listed 'Best First'.
RE: Re: List processing performance
by btrott (Parson) on Jul 11, 2000 at 20:52 UTC
    Oh, is *that* what we're doing then. :) In which case replace your last line with this:
    @words = sort grep !$seen{$_ = lc}++, @words
    A couple characters shorter, plus it's faster because the sort is done after you've filtered out duplicates and common words.
      Golf, eh? @words = sort grep !$seen{+lc}++, @words; The + keeps lc from being interpreted as a hash key, and lc operates on $_ by default.

      Update: Of course, this works if your data set is all lowercase. Moral of the story, don't go for the birdie unless you're sure it's what you want.

        Yes, but it doesn't work properly, because you're not actually changing $_. And if you don't change $_, then the words don't get lower-cased, and your sort is off (because it'll sort upper-case before lower-case).