Do you know where your variables are? | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Always let your commas trail. And I prefer having a comma even on the last element because then tomorrow when I add another element it's just a new line; I don't have to add a comma to the previous last line; my git diff is cleaner, and I'm less likely to forget. If you are concerned about efficiency please understand that push behaves similarly to the C++ vector push_back method, which has an amortized time complexity of O(1). This is because as more space is allocated, the man behind the curtains asks for more than is needed immediately, predicting that more will be needed eventually. Here's a made up example:
The formula for calculating how much extra space to allocate is not the same as what I demonstrated here. I just wanted to demonstrate that Perl allocates memory in chunks that allow for expansion, and only moves the array to a new, larger block when that future expansion memory in the current block has depleted. Because the need to copy the entire array over to a larger memory block as the array expands only happens infrequently, despite the copy-over process being linear, the amortized time is constant; the copy-over fades into background noise. Dave In reply to Re: Adding items to arrays: best approach?
by davido
|
|