On Mar 26, 8:24 am, [EMAIL PROTECTED] (John W. Krahn) wrote:
<snip>
> Telemachus wrote:
> > So in a context like this, would the following be better?
>
> > for my $num (@testAr) {
> >     if (($num % 2) == 0 ) {
> >         print "$num\n";
> >     }
> > }
>
> That code is iterating over a larger list, assuming that @testAr
> contains *some* odd numbers.

First, thanks for your response. This one bit confuses me: how is the
if iterating over a *larger* list than the grep? Don't they both have
to run through all of @testAr in order to weed out odd numbers? Is
there an advantage to using grep here rather than the foreach?

> I don't know why you would *want* to use grep() in void context that
> can't be done better some other way?

I don't want to - in fact, I want *not* to. My problem was that I
couldn't quite see if "don't use in a void context" amounted to "don't
use grep unless you actually wish to keep the returned items in a new
array." I wasn't sure whether using grep to filter results for
printing was also a no no. As my question above mentions, I'm still
not sure whether there is any advantage to grep or to a foreach loop
with an if-test in such a case. Or have we reached a point where it's
just style and choice?

Thanks.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to