There's more than one way to do things PerlMonks

### Re^2: Yet more While issues

by Dandello (Scribe)
 on Mar 18, 2011 at 05:20 UTC ( #893910=note: print w/replies, xml ) Need Help??

in reply to Re: Yet more While issues
in thread Yet more While issues

Back to square one I guess. This

``` 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];
}
only seems to work relatively short strings, but not when the string gets much more than 500 characters. Then it throws 'uninitialized value in \$offset' errors which I'm guessing means the algorithm didn't find the one 'c' that had to be there.

I admit I'm at my wit's end here. This looks like it should work, but it doesn't. There is a 'c' in every row, so that's not the problem.

THIS works

```sub popnum3 {
my ( \$x, \$y, \$z, \$zazb ) = @_;
if ( \$y == 0 ) {
\$aob[\$x][0] = \$initial * ( 1 + \$z );
}
else {
while (1) {
my \$xda = int rand( \$total + 1 );

if ( substr( \$zazb, \$xda, 1 ) eq 'c' ) {
\$aob[\$x][\$y] = \$aob[\$xda][ \$y - 1 ] * ( 1 + \$z );
last;
}
}
}
return \$aob[\$x][\$y];
}
But takes about 5 seconds per row on the full array and since there's 8400 rows in the full run, that's about 12 hours.

Getting the run time down would be very helpful.

Thanks

Replies are listed 'Best First'.
Re^3: Yet more While issues
by ikegami (Pope) on Mar 18, 2011 at 07:23 UTC

Then it throws 'uninitialized value in \$offset' errors

I don't believe you when you say that snippet produced that warning.

Then it throws 'uninitialized value in \$offset' errors which I'm guessing means the algorithm didn't find the one 'c' that had to be there.

No. If it doesn't find any 'c', it never uses \$offset.

PS - Don't count out half the warning (not even counting the line number).

Create A New User
Node Status?
node history
Node Type: note [id://893910]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-08-24 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (360 votes). Check out past polls.

Notices?