Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Confusing syntax error with grep

by etcshadow (Priest)
on Aug 12, 2004 at 14:15 UTC ( #382248=note: print w/replies, xml ) Need Help??


in reply to Confusing syntax error with grep

Don't try to futz with
grep (expr), @list;
or
grep ((expr), @list);
or any such... you just confuse the hell out of the parser and out of someone trying to read it. Instead use this syntax for grep:
grep { block } @list;
Those are curly braces setting off a block, not parenthesese setting off an expression. So your code would become:
grep { ( -f "$path/$_" ) and ( $_ =~ m/\.(java|xml|cpp|c|h|inc|pas)$/i ) and ( $self->Scan_File( "$path/$_", $out, $target ) } @files ); # Also scan any subdirectories. grep { ( ( -d "$path/$_" ) and ( $_ !~ /^\.+$/ ) and # but avoid . and .. ( $self->parseDir( "$path/$_", $out, $target ) ) } @files;
However, it also doesn't make sense because you are using grep in a void context. grep is meant to filter a list (reduce some of the contents out of list1 into list2). If you just want to apply a block of code against every item in a list, think of using map or foreach. Most folks here will complain, likewise, if you use map in a void context (for the same reason, sort of, that I complain about you using grep... the purpose of map (in perl) is to *mutate* a list... although in some other language paradigms one might think of map as making sense in a void context... but that's another argument).
------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: Confusing syntax error with grep
by revdiablo (Prior) on Aug 12, 2004 at 16:42 UTC
    However, it also doesn't make sense because you are using grep in a void context.

    Not necessarily. The first grep is in void context, but the last one may or may not be -- it depends on how the subroutine is called. My guess is he took some working code and added a second grep at the end, without really realising how it worked.

    Update: It's pointless to guess how it happened. Kindly ignore that sentence. :-)

Re^2: Confusing syntax error with grep
by gumpu (Friar) on Aug 12, 2004 at 17:25 UTC

    "However, it also doesn't make sense because you are using grep in a void context. grep is meant to filter a list (reduce some of the contents out of list1 into list2). If you just want to apply a block of code against every item in a list, think of using map or foreach. Most folks here will complain, likewise, if you use map in a void context"

    Guess I agree on using map instead of grep. But I find foreach ugly.

    foreach ( @alist ) { if ( condition( $_ ) ) { do_something( $_ ); } }

    Is far less pretty to the eye than:

    map { condition( $_ ) and do_something( $_ ) } @alist;

    OK, map ( or grep ) has to create a list which is not used. I used to worry about that too. However, given the size of an list in an average problem and the amount of memory that is nowadays present in a computer it is no longer relevant to worry about it. For instance in my case the list holds the names of all files in a single directory. That will be 1 to 1000 elements or so. Compared to the 256 Mbyte that is available in my computer this is peanuts. So I stopped worrying about it and instead worry about making it readable.

    Of course it would be even nicer if there was a construct in perl like:

    { do_something( $_) } forall ( @alist ) where { condition( $_ ) }

    Have Fun

      Of course it would be even nicer if there was a construct in perl like:    { do_something( $_) } forall ( @alist ) where { condition( $_ ) }
      You mean like: do_something( $_ ) for grep condition( $_ ), @alist; ?

      Update: condition( $_ ) and do_something( $_ ) for @alist; is going to be faster because it doesn't have to loop twice.

        That comes pretty close yes :)

        Have Fun

      Come to think of it:

      foreach ( @alist ) { if ( condition( $_ ) ) { do_something( $_ ); } }

      can be written as

      foreach ( @alist ) { condition( $_ ) and do_something( $_ ) }

      Which is as pretty as the map version. The point about worrying too much about memory still stands though.

      Have Fun

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://382248]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-02-07 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (38 votes). Check out past polls.

    Notices?