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:

@a = qw(a b c d e); # Array has 5 elements, with room for 10; push(@a, qw(f g h i j)); # Array has ten elements, with room for 10. push(@a, qw(k l m n o)); # Array has 15 elements with room for 30. New + memory had to be allocated, and array had to be moved into new, larg +er memory space. push(@a, qw(p q r s t)); # Array has 20 elements with room for 30. push(@a, qw(u v w x y)); # Array has 25 elements with room for 30. push(@a, qw(z 1 2 3 4)); # Array has 30 elements with room for 30. push(@a, qw(5 6 7 8 9)); # Array has to be moved to new memory. Array +has 35 elements with room for 70.

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
in thread Adding items to arrays: best approach? by geertvc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.