in reply to More while issues
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: More while issues
by Dandello (Monk) on Mar 06, 2011 at 16:26 UTC | |
by johngg (Canon) on Mar 06, 2011 at 16:46 UTC | |
by Dandello (Monk) on Mar 06, 2011 at 20:20 UTC | |
by johngg (Canon) on Mar 06, 2011 at 23:27 UTC | |
by Dandello (Monk) on Mar 07, 2011 at 01:10 UTC |