in reply to Re^2: Primes. Again.
in thread Primes. Again.

Not obnoxious at all! Healthy curiosity from a Perl newbie I'd have said.

next is a statement used inside a loop to say "skip the rest of the loop code and start the next itteration".

next if ! length $_;

is a next statement that is modified by an if. It is only executed if the if condition is true.

join concatenates the elements of a list together using the string supplied between each pair of elements.

grep filters a list of elements and only returns the list of elements for which the condition (in {} brackets) evaluates true.

Note that this is a very quick introduction to these concepts. I expect you will take a trawl through the tutorials section now looking for next, last, redo, join, grep, map and modifiers.

You should look for those key words in The Camel too.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^4: Primes. Again.
by Andrew_Levenson (Hermit) on Apr 26, 2006 at 15:47 UTC
    I, for the life of me, could not grasp the "length $_" concept (what it was testing for each entry), so we re-wrote it in a way that worked better for our comprehension.

    use strict; use warnings; my @prime = (2); my $not_prime; for my $i(3..100){ $not_prime=0; for(@prime){ next if int($i/$_)!=($i/$_); $not_prime=1; } push @prime, $i if $not_prime!=1; } print join "\n", @prime;


    Thanks again for your help! (The next, last, and conditional-if's that you inadvertantly taught me are VERY welcomed pieces of information.)

      length returns the length (number of characters) in a string. Because you were setting the string to empty I used length to test for that condition and skip the empty string.

      Others have provided better solutions. I was more interested in keeping to the essential structure of your code and using that as a springboard for teaching a little Perl. I seem to have achieved that. :)


      DWIM is Perl's answer to Gödel