Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

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

The node you're responding to is over 14 years old! Sadly, its celebrated author (the famous davorg) hasn't visited us for more than two years and so is unlikely to respond. BTW, davorg, aka Dave Cross, is former head honcho of London.pm (around the time they sponsored a camel at London zoo), professional Perl trainer for over twenty years, and the author of at least four Perl books. Let's hope he visits us again soon.

Meanwhile, I'll try to explain his masterful one-liner for you. You should read the immortal line:

grep { $_ eq '111' } values %hash
as a "pipeline" from right to left. The call to values %hash returns a list of all values in the hash (see values). The grep function evaluates the { $_ eq '111' } code block for each element of this list (locally setting $_ to each element), returning a list consisting of those elements for which the expression evaluated to true. That is, it returns a list of all values in %hash which have the exact value 111.

Note that hashes have unique keys but their values are not unique, so the list returned by this expression may have more than one element; the above expression will only tell you how many times the value 111 appears in the hash but will not tell you the keys of the hash with that value -- for that, you would need to iterate through the keys of the hash instead, checking each value in turn (as the legend davorg did in his second code snippet, using the keys function).

Now I've answered your question, you gotta tell me why you were asking a question about such an ancient node (I won't be able to sleep tonight not knowing).


In reply to Re^3: grep value in hash by eyepopslikeamosquito
in thread grep value in hash by kanish

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 about the Monastery: (3)
As of 2024-04-23 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found