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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.