Re: Semcarlbd letrtes sepur! (Scrambled letters super!)

Jasper McCrea <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Organization GuideGuide
Message-ID <[email protected]>
Kripa Sundar wrote:
> 
> Bart writes:
> 
> => Especially the idea of using the /\B/ anchor appeals to me.
> =>  Hmm... let's try it:
> =>
> => : perl -pe 'sub r{join"",map chop,sort map{rand.$_}shift=~/(.)/g};s/\B(\w+)\B/r$1/eg'
> 
> Thanks to Etienne for the "map chop,sort map".  That was
> neat.  At long last, we are having some FWP on this list.  :-)
> 
> Bart's "\B" is great, too.
> 
> Now we can inline &r, and save some chars.
> 
>   || perl -pe 's/\B(\w+)\B/join"",map chop,sort map{rand().$_}split"",$1/eg'
> 
> I have brought back Etienne's parens for rand(), to avoid
> the warning.  I've also replaced the /(.)/g in &r with a
> split().  It is just as long, and is aesthetically pleasing
> (to me), because I don't have a m// in the RHS of an s///.

Never mind aesthetics. This is golf. An alternative is:

  perl -pe 's#\B\w+\B#join"",sort{5-rand 10}$&=~/./g#eg'

uses a different rand sort. '.5-rand' unfortunately doesn't work. I guess
the sort is looking for < -1, then -1 < x < 1, then > 1, and not simply
-ves and +ves. Whatever, this is only two characters longer.

By the way, the error message from rand.$_ can be eliminated in one less
character using -X instead of ().

Jasper
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.