<div class="gmail_quote">On Fri, Nov 12, 2010 at 11:05 AM, Jacinta Richardson <span dir="ltr">&lt;<a href="mailto:jarich@perltraining.com.au">jarich@perltraining.com.au</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">Alfie John wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Be careful to think that this is absolutely true. Perl&#39;s arrays are not C arrays. If your index is greater than the number of elements in the array, then the array will be extended i.e. the array element pointers will be copied into a new array which can hold just one more element. This will happen *each time* you extend the size of an array i.e. push()!<br>

</blockquote>
<br></div>
This is not completely correct.  When you create an array in Perl, Perl creates a hunk of memory for it and puts the array in the middle.  If you unshift or push things onto the array they bring the start or end of the array closer to the boundaries of the memory Perl put aside for you.  If you get &quot;too close&quot; to either end, then Perl allocates twice as much space for you (as it did the first time) and copies your array into the middle of that.  You can then add to the start or end of the array at will until you again get &quot;too close&quot; to one of the ends; wherein Perl gives you more space again.<br>
</blockquote><div><br>I remember years ago someone saying something along the lines that unshift() was more expensive than push() so at the time I had a cursory look at how Perl implemented arrays. My explanation above was how I understood the code at the time. Looking again tonight I can see that I was wrong! Sorry Daniel and thanks Jacinta for picking that up :)<br>
<br>Alfie<br></div></div>