On Mar 7, 2004, at 8:55 AM, Stuart White wrote:

This was my first chance at testing this, and it did
work.  How, I'm not sure though.

Then, I'm glad you asked. Let's take a closer look.


Specifically:

What do you call the "PRIMES:" and "__END___" syntax?
I'll look up what it's purpose is, I just don't know
it's name.

Let's start with the easy one, __END__. There's no magic going on here, you're program will run fine without it. Consider it a note to perl that reads, "My code stops here, don't read on." I add it to programs I post into messages like this, so you and/or perl, can tell where the code I posted ends.


PRIMES: ... is a label. I'm labeling the first loop, so I can make my call to next() push that loop along, instead of the one I'm currently in. By default, next() works on the immediately enclosing loop.

Next, I don't quite understand the for loops.  I
understand that you created two scalars,
$possible_prime and $test within the scope of the for
loops, and I assume you did that because they are not
needed outside of the for loops, right?

You got that right, yes.


That part I understand.  However, I don't understand this part of
the line:
for my $possible_prime (@primelist)
If I were to write it, I'd declare $possible_prime at
the top where all of my variables are declared.  And
then I'd write it like this:
foreach (@primelist)
Would that be a correct representation?

foreach (@primelist) goes through @primelist one at a time, sticking each element of the array in the default variable $_. The only difference in my loop, is that I ask the elements be stuck in $possible_primes instead. Otherwise, the two are functionally identical.


The reason I didn't use the default variable, is because we're talking about nested foreach loops here. If they both stuck their values in the same place, the inside loop would clobber the outside loops values and I would have no way to access both.

I'm also not sure how $possible_prime gets a value for
the range operator to work on it in the second for
loop.  How does it get a value?

I think I just answered this above, but let me know if you're still stuck.


The other constructs that are confusing me are next
and unless.  I've read about them, I've just not used
them.  I remember reading that unless can be used in
place of while, where the block of code is before
using unless, instead of after like in while.  That's
correct, right?

No, unless is just an if with the condition negated. In other words, the following are identical:


if ( not( some_conditional_test ) )

unless ( some_conditional_test )

I'll have to look up next.  I know
when I first read about it, I didn't understand it, so
I've avoided attempting to use it.

next() jumps to the next iteration of the target loop, or the enclosing loop, by default. Consider this example:


foreach (1..10) {
        next if $_ % 2;         # skip to the next number, if this one is odd
        print "$_\n";         # this prints just even numbers
}

Hope that clears up the rest of your confusions.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to