grep'd list as loop parameter

Discussion in 'Perl Misc' started by ed, Sep 3, 2003.

  1. ed

    ed Guest

    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
     
    ed, Sep 3, 2003
    #1
    1. Advertising

  2. ed

    John Bokma Guest

    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/
     
    John Bokma, Sep 3, 2003
    #2
    1. Advertising

  3. ed

    ed Guest

    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
     
    ed, Sep 3, 2003
    #3
  4. ed

    Anno Siegel Guest

    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
     
    Anno Siegel, Sep 3, 2003
    #4
  5. ed

    Anno Siegel Guest

    ed <> 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
     
    Anno Siegel, Sep 3, 2003
    #5
  6. ed

    ed Guest

    On 3 Sep 2003 19:15:27 GMT, -berlin.de (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
     
    ed, Sep 3, 2003
    #6
  7. ed

    John Bokma Guest

    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/
     
    John Bokma, Sep 3, 2003
    #7
  8. ed

    John Bokma Guest

    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/
     
    John Bokma, Sep 3, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    625
    benben
    Jan 31, 2006
  2. Replies:
    4
    Views:
    549
    Matteo
    Mar 29, 2007
  3. Roedy Green
    Replies:
    3
    Views:
    455
    Mike Schilling
    Sep 13, 2008
  4. Replies:
    1
    Views:
    159
    Bob Barrows [MVP]
    Jul 19, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    452
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page