SPUG: Grep syntax
DeRykus, Charles E
charles.e.derykus at boeing.com
Fri Jun 15 10:03:44 PDT 2007
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.
I tend to agree although there have been several conterpoints in the
Perl newsgroup about void context.. Someone pointed out that 'print'
for instance returns a success/fail flag and no one ever looks at it :).
I think T.von Parseval even submitted a patch to make 'grep' context-
aware so there was no longer any efficiency issue. Still seems like
unnatural...
> 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;
Ugh, that's so much easier to understand expanded in a simple loop even
with a statement modifier: $_ = 255 for @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)
Not that there's anything wrong with that :) ... but I
still prefer the statement modifier loop which seems so
much more "English-like" to me.
--
Charles DeRykus
More information about the spug-list
mailing list