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