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

Reply via email to