Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Primes. Again.

by Andrew_Levenson (Hermit)
on Apr 26, 2006 at 03:24 UTC ( [id://545680]=note: print w/replies, xml ) Need Help??


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

At the risk of being obnoxious, I must politely request:
If you find the time, could you break down the following lines for me? They are too advanced for me to read.
next if ! length $_;
next if int($i/$_)!=($i/$_) or $i==$_; (I suppose I don't understand 'next')
print join "\n", grep {length $_} @prime;

Thanks.

Replies are listed 'Best First'.
Re^3: Primes. Again.
by GrandFather (Saint) on Apr 26, 2006 at 03:36 UTC

    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
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://545680]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found