Commits

Anonymous committed 517faee

Add an item to the bad elements

  • Participants
  • Parent commits ef8fc19

Comments (0)

Files changed (3)

         - long functions and methods.
         - duplicate code.
     - using map instead of for/foreach/while (each) for its side-effects.
+    - using grep instead of first/any/all/etc or a hash lookup.
 
 * Add a page about modules.
 
-* Page about Bad Elements:
-    - using grep instead of first/any/all/etc or a hash lookup.
-
 Long Term:
 ----------
 

File src/tutorials/bad-elements/index.html.wml

 
 </item>
 
+<item id="grep_instead_of_any" h="Using grep instead of any and friends">
+
+<p>
+Sometimes one can see people using <pdoc_f f="grep">grep</pdoc_f> to find
+the first matching element in an array, or whether such an element exists at
+all. However, grep is intended to extract <b>all</b> matching elements out
+of a list, not just the first one, and as a result will not stop until it
+finds them all. To remedy this look at either <tt>first()</tt> from
+<cpan_self_mod m="List::Util" /> (to find the first match) or 
+"any/all/notall/none" from <cpan_self_mod m="List::MoreUtils" /> (to find
+whether a single element exists). These better convey one's intention
+and may be more efficient because they stop on the first match.
+</p>
+
+<p>
+One should note that if one does such lookups often, then they should try
+to use a <a href="$(ROOT)/topics/hashes/">hash</a> instead. 
+</p>
+
+</item>
 </main_list>
 
 #include "bad-elements-sources.wml"