Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

"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

In reply to Re^2: Confusing syntax error with grep by gumpu
in thread Confusing syntax error with grep by gumpu

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2023-02-06 08:46 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.