SPUG: Grep syntax

Ivan Heffner iheffner at gmail.com
Fri Jun 15 23:07:51 PDT 2007

All of this talk (and disdain) for grep. Why?

And no one has mentioned it's counterpart: map

my @b_copy = map { 255 } @b;

I believe that is about as clear as it gets. No worries about scope. No use
of modification of an array in place. I know that the ()x idiom does the
same thing. Unfortunately, my mind pulls map and grep up more readily than
the x operator and when it does, I usually have to test whether I have the
context correct for what I'm trying to do. map is very unambiguous: it
always takes a list and always returns a list. (Now is when Yitzchak or Josh
ben Jore will tell me how I'm wrong....)


P.S. - I've really enjoyed the last couple of active threads on this list. I
like to see the discussions and see what various opinions are in the
community. Thanks to everyone for sharing.

On 6/15/07, Bill Campbell <bill at celestial.com> wrote:
> On Fri, Jun 15, 2007, John W. Krahn wrote:
> >Bill Campbell wrote:
> >>
> >> The author of the perl (Net::CIDR from CPAN) seems to like using grep
> as a
> >> method of processing arrays while doing no regular expression
> checking.  He
> >> also does things like this to create an array, @bcopy, the same size as
> @b,
> >> populated with 255.  This also takes advantage of perl's ``magic''
> where
> >> looping through an array allows one to modify elements of the array by
> >> manipulating $_;
> >>
> >> my @bcopy = @b;
> >> grep { $_ = 255 } @bcopy;
> >
> >Or you could do that like this:
> >
> >$_ = 255 for my @bcopy = @b;
> My biggest problem with this is that, without comments, many people
> wouldn't realize that ``$_ = 255'' in this loop context is modifying the
> contents of the @bcopy array.  I must admit that I've used this
> ``feature''
> of perl many times, but I don't find it intuitively obvious.
> Would bcopy still exist outside of this statement?  I would think that the
> ``my'' in the loop would be interpreted as local to the loop.
> I tend to write code defensively, and break things into steps to be sure
> that the language is doing what I want, something like this:
> my @bcopy = ();
> for (@b) {
>         push(@bcopy, 255);
> }
> There's no possible ambiguity with this.
> Bill
> --
> INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
> URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
> FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206)
> 236-1676
> ``I have no reason to suppose that he, who would take away my Liberty,
> would
> not when he had me in his Power, take away everything else.''  John Locke
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
>      POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>     MEETINGS: 3rd Tuesdays
>     WEB PAGE: http://seattleperl.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20070615/5c6d4adc/attachment.html 

More information about the spug-list mailing list