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/