Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your options are near limitless (I've outlined five approaches here) but your strategy will be dictated by exactly what your specific needs and goals are. (all examples will convert @array to have no more than $N elements)</p> <hr> <p>[EDIT]</p> <p>As others have pointed out, the way suggested in the original question is actually not deprecated, and it provides the fastest, tersest, but not necessarily the most readable solution. <strong>It also has the side effect of expanding an array of fewer than $N elements with empty elements</strong>:</p> <pre><code>$#array = $N-1; </code></pre> <hr> <p>Least code: </p> <pre><code>#best for trimming down large arrays into small arrays @array = $array[0..($N-1)]; </code></pre> <p>Most efficient for trimming a small number off of a large array:</p> <pre><code>#This is a little less expensive and clearer splice(@array, $n, @#array); </code></pre> <p>Undesirable in almost all cases, unless you really love delete():</p> <pre><code>#this is the worst solution yet because it requires resizing after the delete while($N-1 &lt; $#array) { delete(array[$i]); } </code></pre> <p>Useful if you need the remainder of the list in reverse order:</p> <pre><code>#this is better than deleting because there is no resize while($N-1 &lt; $#array) { pop @array; #or, "push $array2, pop @array;" for the reverse order remainder } </code></pre> <p>Useful for saving time in long run:</p> <pre><code>#don't put more values into the array than you actually want </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload