SPUG: Petty perl obfuscation question
Yitzchak Scott-Thoennes
sthoenna at efn.org
Sun Oct 13 19:54:52 CDT 2002
On Sat, 12 Oct 2002 19:57:16 -0700 (PDT), ced at carios2.ca.boeing.com wrote:
>
>> $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.
>
>Not nearly as elegant... but avoids the join ;)
>
>my($p, at s) = $string=~/(.*-|..)/g;$p.=$_ for sort @s;$p
That's longer than the join would have been.
The simple solution:
sub TokenSorter{my($p, at t)=pop=~/.*-|../g;join'',$p,sort at t}
is pretty hard to beat.
After some effort I came up with:
sub TokenSorter{substr join('',sort"-$_[0]"=~/.*-|../g),1}
sub TokenSorter{${\(($_=pop)=~/.*-/gc)}.join'',sort/../g}
sub TokenSorter{$_=pop;(/.*-/gc,"$&").join'',sort/../g}
sub TokenSorter{@_=pop=~/.*-|../g;join'',shift,sort at _}
If the OP was more interested in obfuscation than brevity, here's an attempt
(doesn't work if prefix can contain a '-'; doesn't work on EBCDIC):
sub TokenSorter { local$";
"$${\reverse\split'-'=>$_[0]}-@{[(${chr(34)}=''),sort+pop=~/(?:.*-)?(..)/g]}"
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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