Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Perl question

Name: Anonymous 2012-09-04 13:05

What the hell is going on in this line (taken from the first example here http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Perl):
>next if $tested[$j++];

This is incrementing the number somehow but I have no idea HOW. My first thought was that it was testing if there existed a number in the position after this one and then moving to the next pass of the while loop if that was true. But if that's the case, then how does it ever increment?

So my new thinking is that, based on other stuff I'm finding online and playing around with it, this is incrementing $j to ($j+1), creating $tested[$j+1] in the process. Then, if $tested[$j] has a number in it, it moves on to the next pass of the loop. If it doesn't have a number in $tested[$j], then it pushes $j onto the primes list? Do I have this right?

So what this whole thing really means is, if there's a 1 in $tested, that means that position's corresponding number has already been evaluated to be not-a-prime, because it's a multiple of some other number. Is that right?

Name: Anonymous 2012-09-04 13:10

Oh wait nevermind, I'm an idiot. It will increment $j to $j+1 after returning $j. But I think the rest is right.

Name: Anonymous 2012-09-04 13:25

YES, YOURE IDIOT

Don't change these.
Name: Email:
Entire Thread Thread List