Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

From the documentation on Perl grep():

Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value con +sisting of those elements for which the expression evaluated to tr +ue. In scalar context, returns the number of times the expression + was true.

So you are making a new list, @primes, from the original list, @numbers, including only those elements of @numbers for which the expression returns true.

Your expression,

{ ( exists $is_prime{$_} and $is_prime{$_} ) or ( $is_prime{$_} = is_prime($_) ) }

has two conditions. If either of those returns true for the number from @numbers currently being evaluated, the number is added to @primes.

As grep goes through @numbers, it assigns each one to $_ as it is working with it.

The first condition tests to see whether there is already an entry in the hash %is_prime for the current number:

exists $is_prime{$_}

If there is not, the second part of the first condition, after the and, is ignored and grep() goes on to the second condition. If the number does exist as a key in the hash,

and $is_prime{$_}

checks to see whether additionally the value in the hash is true (ie not 0 or undef).

If that first condition returns true, the second condition is ignored. If the first condition is false, the second condition calls the sub is_prime() with the number:

is_prime($_)

. . . and if the sub returns true, the second condition both adds the number to the hash:

$is_prime{$_} = # the assigned value is the result of is_prime($_) if that function re +turned true

. . . and causes the expression to return true, which adds the number to @primes.

Hope this helps.

The way forward always starts with a minimal test.

In reply to Re: Unable to Understand grep and hash use for prime no. by 1nickt
in thread Unable to Understand grep and hash use for prime no. by shankonit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found