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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.