Commits

shl...@13c2ddb8-3aa7-4284-800b-8db07c650eef  committed 34d2a61

Add the $#array.

  • Participants
  • Parent commits 42620a4

Comments (0)

Files changed (3)

     - using $a and $b as variable names.
     - last/next/redo/etc. without explicit LABELs.
     - Add a note about using $ARGV[0] and $ARGV[1].
+    - abuse of $#array (like $#array + 1 for array length).
 
         - also open.
     - comments and identifiers in a foreign language. 
     - STDIN instead of ARGV.
-    - abuse of $#array (like $#array + 1 for array length).
     - interpolating strings directly into regex.
         - either comment that you want to accept a regex or use \Q and \E.
+    - $array[$#array] instead of $array[-1]
 
 * Link to Fomberg's Hebrew with Perl site.
 

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

 
 </item>
 
+<item id="abuse_of_array_last_index" h="($#array + 1) and Other Abuses of $#.">
+
+<p>
+The <tt>$#array</tt> notation gives the last index in <tt>@array</tt> and
+is always equal to the array length minus one. Some people use it to signify
+the length of the array:
+</p>
+
+<pre>
+\# Bad code:
+my @flags = ((0) x ($#names +1))
+</pre>
+
+<p>
+However this is unnecessary because one can better do it by evaluating
+<tt>@names</tt> in scalar context, possibly by saying <tt>scalar(@names)</tt>.
+</p>
+
+<pre>
+\# Better code.
+my @flags = ((0) x @names);
+</pre>
+
+</item>
+
 </main_list>
 
 #include "bad-elements-sources.wml"