Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Condensing a grep into a sort

by Roy Johnson (Monsignor)
on Jun 09, 2009 at 17:26 UTC ( #770019=note: print w/replies, xml ) Need Help??

in reply to Condensing a grep into a sort

It is not reasonable to have a sort that excludes members of the list entirely. Sort rearranges lists; grep filters them.

What you might want to do is a Schwartzian Transform so that the regex doesn't need to be applied to $a and $b repeatedly.

print map {"$_->[0]\n"} sort {$a->[1] <=> $b->[1]} map { [$_, (/C(\d*)/)[0]] } grep { /P/} @compounds;
Hoping for partial credit, I eliminated the join.

Caution: Contents may have been coded under pressure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://770019]
[ambrus]: I really don't like automagic stuff. I'm happy when computers do exactly what I tell them, even if that means they sometimes do the wrong thing.
[ambrus]: And I don't much like syntax highlighters. If you need a syntax highlighter to understand your code, then your code is written unclear.
[ambrus]: And if you need a syntax highlighter to color parenthesis green and numbers black and letters blue, then you're using the wrong font.
[ambrus]: I have to tolerate syntax highlighters when other people use them, but I don't use them myself. And sorry for the rant.
[GotToBTru]: I appreciate the ability to highlight matching brackets/ parentheses/ braces, both for my own code and the inconsistently indented code of others
[Corion]: I like editors that automatically highlight the matching parenthesis (like % in vi), but that's roughly the extent to which I like editor support ;)
[GotToBTru]: same here Corion.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2017-02-27 12:47 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.