in reply to Obscuring a String
-Tonuse strict; my $string = 'My name is Bob.'; my $percent = 0.5; my @obs = ('a'..'z'); # This array better be larger that length($stri +ng) * $percent my @stringArray = split(//, $string); # break the string up my @positions = (0..(length($string) - 1)); # the locations that will + be swapped my $totalSwaps = int(length($string) * $percent); srand(1); _shuffle(\@positions); _shuffle(\@obs); for (my $i = 0; $i < $totalSwaps; ++$i) { $stringArray[$positions[$i]] = $obs[$positions[$i]]; } $string = join('', @stringArray); print $string . "\n"; sub _shuffle($) { my $aref = shift; my $pos; for(my $i = 0; $i < scalar(@$aref); ++$i) { $pos = int(rand(scalar(@$aref))); ($aref->[$i], $aref->[$pos]) = ($aref->[$pos], $aref->[$i]); } }
|
---|