Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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:
yayaaaaaaayaaaayyayaaayayaayyyaayyyaaaaayaayayyaacaayaaayaayaayaaayyaayyyyaayayyaayaaayaaaaaayyaaayayaaaayaaaaaaaayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxx

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; }
works.

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


Comment on Re^6: More while issues
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://891730]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-10-21 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls