Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: How do I insert an element into an arbitrary (in range) array index?by lhoward (Vicar) |
on Sep 04, 2000 at 00:07 UTC ( [id://30943]=note: print w/replies, xml ) | Need Help?? |
By insert I assume you mean "put between 2 current array
elements, shifting down the later ones to make room".
The unfortunate answer is that you can't do this efficiently using native perl arrays. The code to do it isn't long or complex, but as a consequence of the design of perl's arrays (which makes them so efficient at so many other operations) sacrifices had to be made. One of those is that inserting in the middle of an array is "slow" — the time it takes to do it depends on how many elements are already in the array. (In big-O notation, the operation is said to be O(n).).
In Section
Seekers of Perl Wisdom
|
|