Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > grep'd list as loop parameter

Reply
Thread Tools

grep'd list as loop parameter

 
 
ed
Guest
Posts: n/a
 
      09-03-2003

Hi all. Up until recently I've been avoiding things like grep and
map.
But now I'm starting to use them, and really see their appeal.

I've read the faq: "What's wrong with using grep or map in a void
context?"
But was wondering if anyone could point me to examples of these
functions
being used in void and non-void context.

For example, is the code below ok?
The list grep creates _is_ used, but it isn't assigned to a variable.

foreach my $currentFilePath ( grep{!/^\.{1,2}$/} readdir(DIR) )
{ # .. do stuff with $currentFilePath
}

tia,
--ed
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      09-03-2003
ed wrote:

> Hi all. Up until recently I've been avoiding things like grep and
> map.
> But now I'm starting to use them, and really see their appeal.
>
> I've read the faq: "What's wrong with using grep or map in a void
> context?"
> But was wondering if anyone could point me to examples of these
> functions
> being used in void and non-void context.
>
> For example, is the code below ok?
> The list grep creates _is_ used, but it isn't assigned to a variable.


which is ok since you are *using* the result.

> foreach my $currentFilePath ( grep{!/^\.{1,2}$/} readdir(DIR) )
> { # .. do stuff with $currentFilePath
> }


If you throw away the value(s) generated by map/grep why bother
generating them? It costs memory and probably has other overhead too.

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/

 
Reply With Quote
 
 
 
 
ed
Guest
Posts: n/a
 
      09-03-2003
On Wed, 03 Sep 2003 20:28:53 +0200, John Bokma
<> wrote:

>ed wrote:
>
>> Hi all. Up until recently I've been avoiding things like grep and
>> map.
>> But now I'm starting to use them, and really see their appeal.
>>
>> I've read the faq: "What's wrong with using grep or map in a void
>> context?"
>> But was wondering if anyone could point me to examples of these
>> functions
>> being used in void and non-void context.
>>
>> For example, is the code below ok?
>> The list grep creates _is_ used, but it isn't assigned to a variable.

>
>which is ok since you are *using* the result.
>
>> foreach my $currentFilePath ( grep{!/^\.{1,2}$/} readdir(DIR) )
>> { # .. do stuff with $currentFilePath
>> }

>
>If you throw away the value(s) generated by map/grep why bother
>generating them? It costs memory and probably has other overhead too.


I figured it was ok since I'm looping over the values. Guess not?

So I'd be better off with this? :

foreach my $currentFilePath ( readdir(DIR) )
{ next if ($currentFilePath eq '.' || $currentFilePath eq '..');

# .. do stuff..
}


tia,
--ed
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-03-2003
John Bokma <> wrote in comp.lang.perl.misc:
> ed wrote:
>
> > Hi all. Up until recently I've been avoiding things like grep and
> > map.
> > But now I'm starting to use them, and really see their appeal.
> >
> > I've read the faq: "What's wrong with using grep or map in a void
> > context?"
> > But was wondering if anyone could point me to examples of these
> > functions
> > being used in void and non-void context.
> >
> > For example, is the code below ok?
> > The list grep creates _is_ used, but it isn't assigned to a variable.

>
> which is ok since you are *using* the result.
>
> > foreach my $currentFilePath ( grep{!/^\.{1,2}$/} readdir(DIR) )
> > { # .. do stuff with $currentFilePath
> > }

>
> If you throw away the value(s) generated by map/grep why bother
> generating them? It costs memory and probably has other overhead too.


Just wait for Abigail to see this!

She would be right, of course, in saying that it is Perl's job not
to generate the list in void context, not the programmer's to avoid
calling it that way. However, these days it is so easily avoided in
most cases that it is hardly an issue.

Anno
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-03-2003
ed <coo_t2-NO-LIKE-> wrote in comp.lang.perl.misc:
> On Wed, 03 Sep 2003 20:28:53 +0200, John Bokma
> <> wrote:
>
> >ed wrote:
> >
> >> Hi all. Up until recently I've been avoiding things like grep and
> >> map.
> >> But now I'm starting to use them, and really see their appeal.
> >>
> >> I've read the faq: "What's wrong with using grep or map in a void
> >> context?"
> >> But was wondering if anyone could point me to examples of these
> >> functions
> >> being used in void and non-void context.
> >>
> >> For example, is the code below ok?
> >> The list grep creates _is_ used, but it isn't assigned to a variable.

> >
> >which is ok since you are *using* the result.
> >
> >> foreach my $currentFilePath ( grep{!/^\.{1,2}$/} readdir(DIR) )
> >> { # .. do stuff with $currentFilePath
> >> }

> >
> >If you throw away the value(s) generated by map/grep why bother
> >generating them? It costs memory and probably has other overhead too.

>
> I figured it was ok since I'm looping over the values. Guess not?


No, no, your use is perfectly okay, and quite idiomatic. You are
using the result of grep(), even if you don't assign it to anything
(directly).

Put another way, you're using grep in list context. The arguable
use is in void context.

[unnecessarily corrected code snipped]

Anno
 
Reply With Quote
 
ed
Guest
Posts: n/a
 
      09-03-2003
On 3 Sep 2003 19:15:27 GMT, (Anno
Siegel) wrote:

>
>No, no, your use is perfectly okay, and quite idiomatic. You are
>using the result of grep(), even if you don't assign it to anything
>(directly).
>
>Put another way, you're using grep in list context. The arguable
>use is in void context.
>
>[unnecessarily corrected code snipped]
>
>Anno


I guess I misunderstood.
Thanks.

--ed

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-03-2003
ed wrote:

> On Wed, 03 Sep 2003 20:28:53 +0200, John Bokma
> <> wrote:
>
>
>>ed wrote:
>>
>>
>>>Hi all. Up until recently I've been avoiding things like grep and
>>>map.
>>>But now I'm starting to use them, and really see their appeal.
>>>
>>>I've read the faq: "What's wrong with using grep or map in a void
>>>context?"
>>>But was wondering if anyone could point me to examples of these
>>>functions
>>>being used in void and non-void context.
>>>
>>>For example, is the code below ok?
>>>The list grep creates _is_ used, but it isn't assigned to a variable.

>>
>>which is ok since you are *using* the result.
>>
>>
>>>foreach my $currentFilePath ( grep{!/^\.{1,2}$/} readdir(DIR) )
>>>{ # .. do stuff with $currentFilePath
>>>}

>>
>>If you throw away the value(s) generated by map/grep why bother
>>generating them? It costs memory and probably has other overhead too.

>
>
> I figured it was ok since I'm looping over the values. Guess not?


Yes, it is ok, you are using the values generated by grep. You use grep
to obtain every item in the dir that is neither . nor .. (the regexp
could be written as /^\.\.?$/ btw)

> So I'd be better off with this? :
>
> foreach my $currentFilePath ( readdir(DIR) )
> { next if ($currentFilePath eq '.' || $currentFilePath eq '..');


It *could* be faster to use eq instead of the regexp, but you can do
this in the grep as well. You are not using grep in a void context.

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-03-2003
Anno Siegel wrote:

> John Bokma <> wrote in comp.lang.perl.misc:
>

[snip]
>>If you throw away the value(s) generated by map/grep why bother
>>generating them? It costs memory and probably has other overhead too.

>
> Just wait for Abigail to see this!
>
> She would be right, of course, in saying that it is Perl's job not
> to generate the list in void context


I agree.

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Parameter List / Parameter Block / Anything patterns... mast2as@yahoo.com C++ 4 03-29-2007 09:37 PM
help with parameter not passed from for loop to while loop pauljturner99@gmail.com ASP General 1 07-19-2006 11:06 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57