[ale] Perl Question
Danny Cox
danscox at mindspring.com
Thu Jul 31 18:02:39 EDT 2003
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!
--
kernel, n.: A part of an operating system that preserves the
medieval traditions of sorcery and black art.
Danny
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list