in reply to Re^2: More while issues
in thread More while issues
Using print with a newline instead of say, which was introduced in Perl 5.10, should have no effect on the result. Perhaps you could post what you tried as you might have made a slight error when implementing the method.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: More while issues
by Dandello (Monk) on Mar 06, 2011 at 20:20 UTC | |
For this test I have the $chntot and $incrsdel set so no 'c's will be removed. This line: caaaaaaayyyyyccyyyyyyyyycyyyyyycycyyyyycyyyyyyyyccyyyyyyyyyycyycyyyyyyyyyyyyyyyyyyyyycyyycyyyyycyyyyyyyyyyyyyyyycyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxx This line: caaaayyyyyyyyccyyyyyyyyycyyyyyycycyyyyycyyyyyyyyccyyyyyyyyyycyycyyyyyyyyyyyyyyyyyyyyycyyycyyyyycyyyyyyyyyyyyyyyycyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxx But the kicker is several lines above these two: caaaaaaaaaaaaccaaaaaaaaacaaaaaacacaaaaacaaaaaaaaccaaaaaaaaaacaacaaaaaaaaaaaaaaaaaaaaacaaacaaaaacaaaaaaaaaaaaaaaacaaaaaaayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxx | [reply] [d/l] |
by johngg (Canon) on Mar 06, 2011 at 23:27 UTC | |
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.
I hope this will help you move onward with your code. Cheers, JohnGG | [reply] [d/l] [select] |
by Dandello (Monk) on Mar 07, 2011 at 01:10 UTC | |
Copying and using the entire subroutine above now gets me a line that should only have 'c's, 'y's and 'x' as: It also throws a lot of 'undefined value in $offset' and 'splice offset past end of array' warnings - which is why I had put push @cPosns, pos $str while $str =~ m{(?=c)}g; within a while statement. It doesn't throw warnings and does convert the proper number of 'a's and 'c's into 'y's. But it also doesn't give me my necessary randomness. All the 'y's are 'right' aligned. As for what happens if there aren't enough 'c's? - That possibility has been caught well before this subroutine. The $chntot was still listed as I have been a<->b-ing between my older (working) subroutine and this one. The other subroutine calls for $yb, $incrsdel and $chntot. While the idea of getting the position of the 'a's and 'c's and pulling the random replacements from that looks promising as being more efficient, especially on long strings, the older working subroutine of works. I'll keep playing with the pos function idea. I might be able to get it to work - thanks. | [reply] [d/l] [select] |