How about finding the positions of the 'a' and 'c' characters using a regex look-ahead in a global match along with pos and then randomly replacing 'a's at those positions until there are none left before, if necessary, going on to the 'c's.
use strict; use warnings; use feature qw{ say }; my $str; my $minY; $str = q{accyyaycayaccyyaaycy}; $minY = 15; say $str; my $yCt = $str =~ tr{y}{}; my @aPosns; push @aPosns, pos $str while $str =~ m{(?=a)}g; my @cPosns; push @cPosns, pos $str while $str =~ m{(?=c)}g; while ( ( $yCt < $minY ) && @aPosns ) { my $offset = splice @aPosns, rand @aPosns, 1; substr $str, $offset, 1, q{y}; $yCt ++; } while ( ( $yCt < $minY ) && @cPosns ) { my $offset = splice @cPosns, rand @cPosns, 1; substr $str, $offset, 1, q{y}; $yCt ++; } say $str;
This produces
accyyaycayaccyyaaycy yycyyyycyyyccyyyyycy
Running this again but with $minY reduced to 10 gives this
accyyaycayaccyyaaycy accyyaycyyaccyyyaycy
I hope this is helpful.
Cheers,
JohnGG
In reply to Re: More while issues
by johngg
in thread More while issues
by Dandello
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |