SPUG: Petty perl obfuscation question

ced at carios2.ca.boeing.com ced at carios2.ca.boeing.com
Tue Oct 15 13:09:14 CDT 2002


>> $string = "Prefix-3C2B1A";
>> $string =~ s/(?<=-).+/join'',sort$&=~m{(..)}g/e;
 
>...
>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]}"
|}

Hey, I already bashed my lack of elegance and even added a smiley 
re: not needing a join. 

  [ I was just out pointing another solution that was nearly as short 
    and might appeal to those less familiar with lookbehind assertions 
    and sub eval's.]

BTW, your examples are incredibly instructive.. I had forgotten the 
usefulness of the /c modifer.

Rgds,
--
Charles DeRykus  

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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