Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How to perform different sorts on multiple sections of items in the same array

by james28909 (Deacon)
on Dec 29, 2014 at 03:15 UTC ( [id://1111599]=note: print w/replies, xml ) Need Help??


in reply to How to perform different sorts on multiple sections of items in the same array

Maybe this is what you mean?
use strict; use warnings; my @array = qw(toad frog frig frag crow creep hop); my @sorted; for (@array) { if ( $_ =~ m/.*r/g ) { push( @sorted, $_ ); } } print "$_\n" for sort(@sorted);
Output:
creep crow frag frig frog
EDIT: Missed the point of this might being a homework assignment. apologies

Replies are listed 'Best First'.
Re^2: How to perform different sorts on multiple sections of items in the same array
by GrandFather (Saint) on Dec 30, 2014 at 04:03 UTC

    The .* is not required in your match expression nor is the /g. The whole loop can be replaced by:

    my @selected = grep {/r/} @array;

    Note that neither version of the matching code actually performs a sort so the variable name is more appropriately 'selected' than 'sorted'.

    Perl is the programming world's equivalent of English

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1111599]
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-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found