Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Sorting on identical values (updated)

by AnomalousMonk (Bishop)
on Aug 08, 2019 at 20:31 UTC ( #11104192=note: print w/replies, xml ) Need Help??


in reply to Sorting on identical values

And bringing up the rear, might as well have a GRT example:

c:\@Work\Perl\monks>perl use strict; use warnings; print "perl version $] \n"; my @unsorted = qw( http://myurl.com/search/stringA?SomeMoreStuffA http://myurlA.com/search/stringB?SomeMoreStuffB http://myurlB.com/search/stringC?SomeMoreStuffX http://myurlC.com/search/stringA?SomeMoreStuffXYZ http://myurl.com/search/stringZ?SomeMoreStuffZZZ ); my $delim = '?'; sub decorate { return pack 'a* a a*', m{ /search/ ([^\Q$delim\E]*) } +xms, $delim, $_; } sub undecorate { return m{ [\Q$delim\E] (.*) }xms; } my @sorted = map undecorate(), sort # map { print("== '$_' \n"); $_; } # for debug map decorate(), @unsorted ; print "'$_' \n" for @sorted; __END__ perl version 5.008009 'http://myurl.com/search/stringA?SomeMoreStuffA' 'http://myurlC.com/search/stringA?SomeMoreStuffXYZ' 'http://myurlA.com/search/stringB?SomeMoreStuffB' 'http://myurlB.com/search/stringC?SomeMoreStuffX' 'http://myurl.com/search/stringZ?SomeMoreStuffZZZ'

Update:

sub decorate   { return pack 'a* a a*', m{ /search/ ([^\Q$delim\E]*) }xms, $delim, $_; }
The use of pack in this function is overkill. join achieves the same effect with a little bit less overhead and arguably more clarity:
    sub decorate { return join '', m{ /search/ ([^\Q$delim\E]*) }xms, $delim, $_; }


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2019-09-21 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (270 votes). Check out past polls.

    Notices?