What's the best/preferred/Perl-ish method?
That will be subjective. However, I'd have to go with push for several reasons perhaps the strongest of which is that this is precisely what it is designed to do: append entries to arrays. If it were no better than the first approach it would not be in the language.
Has the first method advantages over the second method or vice versa? What are the advantages, if any?
I see some advantages to the second method:
- There is no need to specify the name of the array twice therefore the programmer is less likely to make a mistake such as extracting data from the wrong array in the RHS.
- The first method does a lot more stuff: extracting data from the array into a list, appending other values and then overwriting the array with the resulting list. This sounds much less efficient than a simple push which (as a guess) I would expect literally to append to the array in situ. For large arrays or many ops this might become an important consideration.
- push will also save you from mistakes like attempting to push to a scalar.
That said, I would definitely combine your 4 pushes into one and probably use a glob or map to avoid all the repetition.
What's the habbit in case of initial array declaration and first method: put the comma's at the back or at the front?
Not sure what you mean here, sorry.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.