in reply to Re: More while issues
in thread More while issues
That doesn't look very deterministic to me! The following code may work a little more efficiently with large strings:
use strict; use warnings; my $incrsdel = 10; my $str = "acbcybaycayacyccy"; my @chrs = split '', $str; my $yTot = $str =~ tr/y//; for my $letter ('a', 'c') { $yTot += subLetter ($letter, $incrsdel - $yTot, \@chrs); last if $yTot >= $incrsdel; } $str = join '', @chrs; print "$str\n"; sub subLetter { my ($letter, $limit, $chrs) = @_; my @letPos = grep {$chrs[$_] eq $letter} 0 .. $#$chrs; my $count = 0; while ($count < $limit && @letPos) { $chrs->[$letPos[my $idx = rand @letPos]] = 'y'; splice @letPos, $idx, 1; ++$count; } return $count; }
Oh, and it scales to a larger set of replaceable characters easily too.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: More while issues
by Dandello (Monk) on Mar 04, 2011 at 03:59 UTC | |
by ynonp (Initiate) on Mar 04, 2011 at 10:28 UTC |