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: <%-{-{-{-<
|
---|
In Section
Seekers of Perl Wisdom