Dandello has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: More while issues
by FalseVinylShrub (Chaplain) on Mar 03, 2011 at 21:39 UTC | |
by Dandello (Monk) on Mar 03, 2011 at 21:49 UTC | |
|
Re: More while issues
by johngg (Canon) on Mar 04, 2011 at 00:07 UTC | |
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 | |
| |
|
Re: More while issues
by Anonymous Monk on Mar 03, 2011 at 21:09 UTC | |
|
Re: More while issues
by Dandello (Monk) on Mar 04, 2011 at 01:19 UTC | |
by GrandFather (Saint) on Mar 04, 2011 at 03:18 UTC | |
by Dandello (Monk) on Mar 04, 2011 at 03:59 UTC | |
by ynonp (Initiate) on Mar 04, 2011 at 10:28 UTC | |
by poj (Abbot) on Mar 04, 2011 at 08:14 UTC | |
|
Re: More while issues
by TomDLux (Vicar) on Mar 04, 2011 at 21:52 UTC | |
by Dandello (Monk) on Mar 05, 2011 at 00:47 UTC | |
|
Re: More while issues
by Anonymous Monk on Mar 04, 2011 at 11:07 UTC | |
by Dandello (Monk) on Mar 05, 2011 at 00:41 UTC |