grep'd list as loop parameter

E

ed

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
 
J

John Bokma

ed said:
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.
 
E

ed

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


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
 
A

Anno Siegel

John Bokma said:
which is ok since you are *using* the result.


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
 
A

Anno Siegel

ed said:
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
 
E

ed

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
 
J

John Bokma

ed said:
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.
 
J

John Bokma

Anno said:
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top