For clarity more than for speed,
should bemy $offset = splice @cPosns, rand @cPosns, 1;
andmy $offset = $cPosns[rand @cPosns];
should simply bewhile (1) { ... last; }
...
For speed, don't copy the zazb argument into a local variable. Access $_[3] directly.
A possible tweak:
push @cPosns, pos $zazb while $zazb =~ m{(?=c)}g;
might be faster as
push @cPosns, $-[0] while $zazb =~ m{c}g;
or maybe even
my $pos = -1; push @cPosns, $pos while ($pos = index($zazb, 'c', $pos+1)) >= 0;
Benchmark and find out.
Which I just can't seem to get my head around to get it to work without throwing 'undefined value' warnings all over the place.
This will occur when 'c' doesn't occur in $zazb. Based on your first snippet, you want
sub popnum3 { my ( $x, $y, $z ) = @_; # $_[3] is $zazb if ( $y == 0 ) { $aob[$x][0] = $initial * ( 1 + $z ); } else { my @cPosns; push @cPosns, $-[0] while $_[3] =~ m{c}g; if (@cPosns) { my $offset = $cPosns[rand @cPosns]; $aob[$x][$y] = $aob[$offset][ $y - 1 ] * ( 1 + $z ); } } return $aob[$x][$y]; }
In reply to Re: Yet more While issues
by ikegami
in thread Yet more While issues
by Dandello
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |