Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^6: More while issues

by Dandello (Scribe)
on Mar 07, 2011 at 01:10 UTC ( #891730=note: print w/replies, xml ) Need Help??

in reply to Re^5: More while issues
in thread More while issues

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

sub rankdwn { my ( $yb, $incrsdel, $chntot,) = @_; my $cnd_a = $aod[$yb] =~ tr/a/a/; my $cnd_y = $aod[$yb] =~ tr/y/y/; my $letter = 'a'; if ( ( $cnd_a + $cnd_y ) <= $incrsdel ) { $aod[$yb] =~ s/a/y/gsxm; $cnd_y += $cnd_a; $letter = 'c'; } while ( $cnd_y < $incrsdel ) { my $xda = int rand( $chntot + 1 ); if ( substr( $aod[$yb], $xda, 1 ) eq $letter ) { substr $aod[$yb], $xda, 1, 'y'; $cnd_y++; } } return; }

I'll keep playing with the pos function idea. I might be able to get it to work - thanks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://891730]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2018-04-21 03:16 GMT
Find Nodes?
    Voting Booth?