in reply to Randomize word
Why expend all those cycles breaking the string into and array and then more cycles to stick'em all back together again :) (It's even fair!)
#! perl -slw use strict; sub shuffleWord { my( $l, $p ) = length( $_[0] )-1; $p = int rand $l - $_ and substr( $_[ 0 ], $_ , 1 ) ^= substr( $_[ 0 ], $_ + $p, 1 ) ^= substr( $_[ 0 ], $_ , 1 ) ^= substr( $_[ 0 ], $_ + $p, 1 ) for 0 .. $l; return $_[ 0 ]; } print shuffleWord $ARGV[ 0 ]; __END__ P:\test>test.pl antidisastablishmentarismmania teinisasdrslaathiaismmtabnnima P:\test>test.pl antidisastablishmentarismmania ibsrniitmsaaadnnaealttiimsmhsa P:\test>test.pl antidisastablishmentarismmania iiarsshaminaaittlmtnbasmsednia
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Randomize word
by go9kata (Novice) on Aug 30, 2013 at 08:08 UTC | |
by BrowserUk (Patriarch) on Aug 30, 2013 at 11:02 UTC |