Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: List processing performance

by autark (Friar)
on Jul 11, 2000 at 20:30 UTC ( [id://22019]=note: print w/replies, xml ) Need Help??


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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://22019]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found