SPUG: Grep syntax
Patrick Michaud
pmichaud at u.washington.edu
Fri Jun 15 09:53:09 PDT 2007
Fortunately, perl allows for something along the lines of
my @foo = (1,2,3,4);
my @foocopy = (255) x @foo;
warn join(',', @foocopy);
or perhaps more clearly:
my @foo = (1,2,3,4);
my @foocopy = (255) x scalar(@foo);
warn join(',', @foocopy);
Some (quite a few, perhaps) perl authors have an unnatural love for grep
and $_, but it doesn't need to be
that way.
-Patrick
On Fri, 15 Jun 2007, Bill Campbell wrote:
> On Fri, Jun 15, 2007, Eric Wilhelm wrote:
>> # from Bill Campbell
>> # on Thursday 14 June 2007 11:39 pm:
>>
>>> $start0=1;
>>> grep { $start0=0 unless $_ == 0; } @a;
>>>
>>> if ( ! $start0 ) { ...
>>
>> ugh.
>>
>> Perhaps that means:
>>
>> my $start0 = ! grep({$_} @a);
>> unless($start0) { ...
>>
>> But that's still awfully non-un-negatedified.
>>
>> if(grep({$_} @a)) { ...
>>
>> Isn't it?
>>
>> And wasn't this a discussion about how you can't do stupid stuff in
>> python? There's nothing perlish about void-context greps and backwards
>> negated logic. IME Perl requires you to do silly things less often
>> than other languages. If the language supports it, you can just say
>> what you mean.
>
> 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;
>
> A python way of doing this is a bit less cryptic. This is similar to the
> perl method of expanding things like 'x' * n with python expanding the
> single element list into a list of length len(b)
>
> # Create array bcopy containing all 255
> bcopy = [255] * len(b)
>
> ...
> 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
>
> ``The fact is that the Constitution was intended to protect us from
> the government, and we cannot expect the government to enforce it
> willingly'' -- Dave E. Hoffmann, Reason Magazine March 2002
> _____________________________________________________________
> 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/
>
More information about the spug-list
mailing list