ww has asked for the wisdom of the Perl Monks concerning the following question:
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)?
|
---|