Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Sorting a subset

by flounder99 (Friar)
on Dec 12, 2003 at 20:04 UTC ( [id://314397]=note: print w/replies, xml ) Need Help??


in reply to Sorting a subset

In your example the second for loop will sort based on the numerical value of array elements with the first character chopped off. If that is what you want then something like this will work:
for my $ref (sort {substr($a,1) <=> substr($b,1)} grep {substr($_,0,1) + eq 'A'} @array) { #whatever with $ref }
If you want to sort asciibeticly just use sort. You don't need to do the substr since the first letter will be 'A' already.
for my $ref (sort grep {substr($_,0,1) eq 'A'} @array) { #whatever with $ref }
A substr will be faster than doing a regex but if you want to make it short you could also write it like this.
for my $ref (sort grep /^A/, @array) { #whatever with $ref }

--

flounder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found