SPUG: Petty perl obfuscation question
Andrew Sweger
andrew at sweger.net
Sat Oct 12 05:48:33 CDT 2002
Assuming well formed input, this seems to work:
$string = "Prefix-3C2B1A";
$string =~ s/(?<=-).+/join'',sort$&=~m{(..)}g/e;
This simply replaces the tokens in place. I had trouble getting it into a
subroutine as one line though. It seems like we should be able to drop the
join() call too.
On Sat, 12 Oct 2002, David Innes wrote:
> #I've managed to shorten all this down to...
>
> sub ShorterTokenSorter { #Two line version
> my ($Prefix, $Tokens) = (shift =~ /^(..*-)(..*)$/);
> return ($Prefix . join('', sort($Tokens =~ /([0-9][A-Z])/ig)));
> }
>
> #Display the output
> print TokenSorter("Prefix-3C2B1A") . "\n";
> print ShorterTokenSorter("Prefix-3C2B1A") ."\n";
--
Andrew B. Sweger -- The great thing about multitasking is that several
things can go wrong at once.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list
mailing list