[ale] Perl Question
David Corbin
dcorbin at machturtle.com
Thu Jul 31 20:21:10 EDT 2003
from "man perlfunc"
grep BLOCK LIST
This is similar in spirit to, but not the same as, grep(1) and
its relatives. In particular, it is not limited to using regular
expressions.
Evaluates the BLOCK or EXPR for each element of LIST (locally
setting $_ to each element) and returns the list value consisting of those
elements for which the expression evaluated to true.
I assume that "grep $doc, at list" is equivalent to "grep {$doc} @list", and your
$doc values were clearly true. The BLOCK version of grep is very useful to
apply more complex "examinations" than standard perl patterns.
On Thursday 31 July 2003 18:02, Danny Cox wrote:
> I was bitten by this yesterday (sorry, I was busy doing other things
> last night, or I would have posted then).
>
> I attempted to use the "grep EXPR, @list" described in "Programming
> Perl" and it didn't work as I expected.
>
> Background: I wanted to set up a list and stuff 3 digit numbers into it
> (actually EDI document names: 810, 850, 208, etc.). I wanted to use
> grep (the Perl grep) to let me know if that number was already extant.
> So, I used the following fragment:
>
> $count = grep $doc, @list
>
> where $doc == "810" for example. When the list was empty, $count was
> 0. When I successfully stuffed something in it, it was always 1,
> regardless of what $doc was and what @list contained. Not what I
> expected.
>
> I then noticed the little bit I missed: EXPR can be a pattern, but it
> doesn't have to be. I changed the frag to:
>
> $count = grep /$doc/, @list
>
> and it worked fine! Glorious!
>
> Now: what the BLEEP was the first form doing, and why would anyone want
> to use it? Having used Unix/Linux since '81, this behaviour totally
> befuddled me for awhile. I mean, grep (the command) always searches for
> patterns, right? This, for me, clearly violates the "path of least
> surprise".
>
> Oh, well, we live and learn.
>
> Perl wizards: please enlighten me!
>
> Thanks!
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list