From: "Mark J. Reed" Date: 2003-09-17T09:12:56+09:00 Subject: Re: scrambler one-liner On Tue, Sep 16, 2003 at 05:04:48PM -0700, Sabby and Tabby wrote: > Xavier Noria wrote: > > > -pe 'gsub!(/\w+/){|w|r=1..w.size-2;w[r]=w[r].split(//).sort_by{rand}.to_s;w}' > > -pe 'gsub!(/\B\w+\B/){|w|w.split(//).sort_by{rand}}' > > In Perl, the criosomapn fnutcoin uesd by srot() is rreqeuid to behave > cisnotsltney, so tihs seohtrr one-lenir may not be ksoher: > > -pe 's!\B\w+\B!join"",sort{.5>rand}$&=~/./g!ge' Right. That's also true of the block passed to Ruby's Array#sort method. The one-liner users Array#sort_by, whose block is used to assign a key to each array element. It's basically a shortcut for a Schwartzian transform: x = y.sort_by { |i| z(i) } is roughly equivalent to this Perl code: $x = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [z($_), $_] } @y -Mark