Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Another of my many blindspots has risen up and bitten me.

I thought I understood grep $person_has eq $_,  $item {...} to mean:

search $person_has for the value of the current default variable (set in a previous statement) and return true if that matches $item.

I've also explored the only alternate meaning I've been able to conjure up... namely:

search through the string $person_has and return true if any part of that string would match any part of $item.

Clearly, my understanding is incorrect, but despite perusing perldoc, Intermediate Perl, Programming Perl, several estimable web resources, -MODeparse and, of course, many of the very numerous nodes found by Super Search comprehension eludes me.

FWIW, this snippet is what's giving me grief:

for $name(@names) { my $hash_key = $name; $person_has = $hash_Dref{$hash_key}; print "\t\t $name has $person_has\n"; # as intended +, thru this line for my $item(@required) { # incorrect b +elow; unless ( grep $person_has eq $_, $item ) { print "$name is missing $item.\n"; } else { print "$name has $person_has.\n"; } } }

...which reports total failure to match any $item to any element of @required. In other words, it executes line 7 in every case, even in cases in which line 6 clearly shows that $person_has includes an exact match for $item.

In the complete script -- which is a lengthy self-imposed elaboration on code developed in Chapter 4 of Intermediate Perl -- I use strict and warnings and receive no reports of either. Further, I have used <c>Data::Dumper<c> and the debugger to verify that the variables contain what I intended/expected.

Can some great teacher lift the scales of blindness from my eyes, by offering a clearer (to me!) explanation of the real meaning of the construct (or, if it pinpoints my misunderstanding, the proper construct)?

In reply to (Mis)Understanding grep...eq $_ by ww

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2022-01-16 20:27 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (49 votes). Check out past polls.