seaver has asked for the wisdom of the Perl Monks concerning the following question:
I wrote this code, and I was challenging myself to condense the code. Basically, I was trying to figure out a way to use regular expressions within the sort in both list and scalar context, and thus not use the grep function.
The catch is, I wanted to do it with less characters than you would find in the grep line.
Can anyone help me?my @compounds=("C3H8O2", "C6H10O5", "C36H62O31", "C5H16N2", "C10H11N5O6P", "C9H11N3O7P", "C3H9N2O2"); print join("\n", sort{($a =~ /C(\d*)/)[0] <=> ($b =~ /C(\d*)/)[0]} grep { $_ =~ /P/} @compounds),"\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Condensing a grep into a sort
by Roy Johnson (Monsignor) on Jun 09, 2009 at 17:26 UTC | |
|
Re: Condensing a grep into a sort
by jwkrahn (Abbot) on Jun 09, 2009 at 17:25 UTC | |
|
Re: Condensing a grep into a sort
by gwadej (Chaplain) on Jun 09, 2009 at 17:33 UTC | |
by seaver (Pilgrim) on Jun 09, 2009 at 19:30 UTC | |
by ikegami (Patriarch) on Jun 09, 2009 at 19:40 UTC | |
by jwkrahn (Abbot) on Jun 09, 2009 at 21:09 UTC | |
|
Re: Condensing a grep into a sort
by Marshall (Canon) on Jun 09, 2009 at 19:40 UTC |