My code
my $cndiea = 0; foreach my $del ( 0 .. $chntot ) { if ( substr( $aod[$yb], $del, 1 ) eq 'y' ) { $cndiea++; } } while ( $cndiea < $incrsdel ) { my $xda = int rand( $chntot + 1 ); if ( substr( $aod[$yb], $xda, 1 ) eq 'a' ) { substr $aod[$yb], $xda, 1, 'y'; $cndiea++; last; } }
As this stands, it works - in a string of 'a's and 'y's, it counts the 'y's and if there aren't enough of them, converts 'a's to 'y's.
Unfortunately, I have to add a second 'layer' as instead of a string with just 'a's and 'y's, I now have a string with 'a's, 'y's, and 'c's. What I need to do is is start converting 'c's to 'y's after all the 'a's have been converted.
But everything I've tried either converts 'c's to 'y's along with the 'a's or it doesn't convert the 'c's at all.
my $cndiea = 0; foreach my $del ( 0 .. $chntot ) { if ( substr( $aod[$yb], $del, 1 ) eq 'y' ) { $cndiea++; } } while ( $cndiea < $incrsdel ) { my $xda = int rand( $chntot + 1 ); if ( substr( $aod[$yb], $xda, 1 ) eq 'a' ) { substr $aod[$yb], $xda, 1, 'y'; $cndiea++; last; } } my $cndi = 0; my $cndj = 0; foreach my $dela ( 0 .. $chntot ) { if ( substr( $aod[$yb], $dela, 1 ) eq 'a' ) { $cndi++; } if ( substr( $aod[$yb], $dela, 1 ) eq 'y' ) { $cndj++; } } if ( $cndi == 0 ) { while ( $cndj < $incrsdel ) { my $xdb = int rand( $chntot + 1 ); if ( substr( $aod[$yb], $xdb, 1 ) eq 'c' ) { substr $aod[$yb], $xdb, 1, 'y'; $cndj++; last; } } }
This version doesn't remove any of the 'c's. If I remove the 'last' from the first 'while' loop it removes the 'c's without going through all the 'a's first but it hangs (goes into an unresolvable loop) if all the 'a's are gone before reaching $cndiea == $incrsdel.
I'm sure fresh eyes see what I'm missing here as I know there has to be a simple solution. And yes, which 'a's and 'c's being converted need to be randomly selected
Thanks in advance.
In reply to More while issues by Dandello
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |