in reply to Perlplexation - foreach shoulda Known
The foreach (and for and map, etc) command can be used to modify the list that it is iterating overI recommend the Effective Perl Programming book to you and your workmates, especially item 20, "Use foreach, map and grep as appropriate", which gives an excellent summary of when to use foreach, map and grep:
- Use foreach to iterate read-only over each element of a list
- Use map to create a list based on the contents of another list
- Use foreach to modify elements of a list
- Use grep to select elements in a list
Note that Hall, McAdams and foy further caution against modifying a list via map:
"For efficiency, $_ is actually an alias for the current element in the iteration. If you modify $_ within the transform expression of a map, you modify the input data. This is generally considered to be bad style, and -- who knows? -- you may even wind up confusing yourself this way. If you want to modify the contents of a list, use foreach."
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perlplexation - foreach shoulda Known
by raybies (Chaplain) on Apr 16, 2012 at 16:34 UTC | |
by eyepopslikeamosquito (Archbishop) on Apr 16, 2012 at 21:09 UTC | |
Re^2: Perlplexation - foreach shoulda Known
by JavaFan (Canon) on Apr 16, 2012 at 11:36 UTC |
In Section
Meditations