Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting arrays ignoring square brackets 1

Status
Not open for further replies.

zumbabumba

Technical User
Mar 4, 2008
8
IT
Hi all. Is there any efficient way to sort arrays ignoring square brackets and/or other characters? Suppose you have:

@names = ('[Ca]rl', 'Bil[l]y', 'Alan');

I want the output of sort @names be: Alan Bil[l]y [Ca]rl. The standard sort outputs [Ca]rl Alan Bil[l]y beacuse square brackets come first in alphabetical order. By now I thought I can copy the names having square brackets in them, remove brackets, sort them, and replace those having brackets, but I guess this is not the perlistic way.
 
With those examples, they sort ok for me, but I get what you're asking. Here's a solution using a Schwarzian Transform:
Code:
@names = map $_->[0],
        sort { $a->[1] cmp $b->[1] }
        map { (my $n = $_ ) =~ tr/a-zA-Z0-9//cd; [ $_, $n ] }
        @names;
 
Thanks Ishnid! It works very well. Now I just have to understand what a Schwarzian Transform is:).
 
The Schwart[z]ian Transform is a well discussed topic, a google search will find many resources that explain it, here is my own attempt to explain it in a perl centric way:


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Didn't they just make a movie about it, big robots and all that?


[jester2]



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top