Perhaps I was a little optimistic when I said "slight error" :-)

In the code that I posted note how I discover the positions of the 'a' and 'c' letters before I start to modify the string. Note also that I don't have to count how many 'a's there are, only 'y's. This is because, according to your spec, the process of turning letters ('a's first then 'c's) into 'y's only continues until there are enough 'y's so the number of them in the string is the crucial factor. You have rather mangled the logic by forgetting about discovering where the 'a's are and assuming that all them will be changed, and then actually doing so willy nilly in a global substitution. You also move the discovery of letter 'c's inside while loop of the letter 'c' replacement stage when it should actually be done once before the string is modified.

In short, your implementation has almost nothing in common with the code I posted so it is perhaps not surprising that the results differ. A few points about your subroutine:

Putting all that together your subroutine might look like this.

... $aod[$yb] = rankdwn( $aod[$yb], $howManyYsDoWeWant ); ... sub rankdwn { my ( $str, $incrsdel ) = @_; my $cnd_y = $str =~ tr/y/y/; my @aPosns; push @aPosns, pos $str while $str =~ m{(?=a)}g; my @cPosns; push @cPosns, pos $str while $str =~ m{(?=c)}g; while ( ( $cnd_y < $incrsdel ) && @aPosns ) { my $offset = splice @aPosns, rand @aPosns, 1; substr $str, $offset, 1, q{y}; $cnd_y ++; } while ( ( $cnd_y < $incrsdel ) && @cPosns ) { my $offset = splice @cPosns, rand @cPosns, 1; substr $str, $offset, 1, q{y}; $cnd_y ++; } return $str; }

I hope this will help you move onward with your code.

Cheers,

JohnGG


In reply to Re^5: More while issues by johngg
in thread 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.