http://www.perlmonks.org?node_id=891730


in reply to Re^5: More while issues
in thread More while issues

Copying and using the entire subroutine above now gets me a line that should only have 'c's, 'y's and 'x' as:
yayaaaaaaayaaaayyayaaayayaayyyaayyyaaaaayaayayyaacaayaaayaayaayaaayyaayyyyaayayyaayaaayaaaaaayyaaayayaaaayaaaaaaaayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxx

It also throws a lot of 'undefined value in $offset' and 'splice offset past end of array' warnings - which is why I had put push @cPosns, pos $str while $str =~ m{(?=c)}g; within a while statement. It doesn't throw warnings and does convert the proper number of 'a's and 'c's into 'y's. But it also doesn't give me my necessary randomness. All the 'y's are 'right' aligned.

As for what happens if there aren't enough 'c's? - That possibility has been caught well before this subroutine.

The $chntot was still listed as I have been a<->b-ing between my older (working) subroutine and this one. The other subroutine calls for $yb, $incrsdel and $chntot.

While the idea of getting the position of the 'a's and 'c's and pulling the random replacements from that looks promising as being more efficient, especially on long strings, the older working subroutine of

sub rankdwn { my ( $yb, $incrsdel, $chntot,) = @_; my $cnd_a = $aod[$yb] =~ tr/a/a/; my $cnd_y = $aod[$yb] =~ tr/y/y/; my $letter = 'a'; if ( ( $cnd_a + $cnd_y ) <= $incrsdel ) { $aod[$yb] =~ s/a/y/gsxm; $cnd_y += $cnd_a; $letter = 'c'; } while ( $cnd_y < $incrsdel ) { my $xda = int rand( $chntot + 1 ); if ( substr( $aod[$yb], $xda, 1 ) eq $letter ) { substr $aod[$yb], $xda, 1, 'y'; $cnd_y++; } } return; }
works.

I'll keep playing with the pos function idea. I might be able to get it to work - thanks.