Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
In general, you shouldn't use grep or map for side-effects.

The purpose of grep is to filter a list into another (possibly shorter) list of elements of the original list. The grep block should really do nothing except evaluate to true or false.

The purpose of map is to transform a list into another list of the same length1, with the transformation being specified by the return value of the map block.

The purpose of 'for/foreach' is to loop over a list and perform an action (perhaps with side-effects such as printing) for each element.

All of these involve looping over the list and, since arbitrary code can be used in the grep or map block, all can be considered as variants on 'for', but with different return values. If you aren't using the return value, you should use for.

All of the 'should' and 'purpose' above relate to communicating your intent to other coders, of course the code would work whichever construct you used for your loop.

1OK, because perl flattens lists, you can change the number of list items by having your block evaluate to a list itself, which is useful to construct a hash from a list of keys, for example.


In reply to Re^4: writtings files to a new folder by jbert
in thread writtings files to a new folder by heidi

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-28 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found