SPUG: Grep syntax
Bill Campbell
bill at celestial.com
Fri Jun 15 09:28:11 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.
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
More information about the spug-list
mailing list