in reply to Build your array with push

I don't like the push way of constructing arrays. I use the following idioms:

my @array = ( "Hibbs", "Daglish", "Schwartz", "Vroom", );

... which works well for adding in any place, and also (un)commenting entries. If I'm sure there are no newlines/whitespace-only elements in the array needed, I also may use the following:

my @array = grep { /\S/ } split /\n/, ' foo bar baz and so on ';

depending on how my input data gets delivered (multi-column Excel data fits better into this model for example).

Replies are listed 'Best First'.
Re^2: Build your array with push
by xdg (Monsignor) on Jan 31, 2006 at 15:12 UTC
    my @array = ( "Hibbs", "Daglish", "Schwartz", "Vroom", );

    I agree. And the little thing that makes this really work well is training oneself to add that trailing comma (after "Vroom") to the last entry. (Ditto for hashes entries, too.)

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^2: Build your array with push
by friedo (Prior) on Jan 31, 2006 at 20:23 UTC
    I don't like the push way of constructing arrays.

    You think that's bad, I once had to maintain a program that did this:

    my @array; $array[$#array] = "Hibbs"; $array[$#array] = "Daglish"; $array[$#array] = "Schwartz"; $array[$#array] = "Vroom"; ...
    And so on for about 30 lines.

    One of the few times I felt justified in regexing some code.

      Woah, woah, woah. That code just replaces the last element multiple times. Did you maybe mean
      $array[@array] = "Hibbs";
      or
      $array[++$#array] = "Hibbs";
      ?

      Caution: Contents may have been coded under pressure.
        Nope, I meant what I said, believe it or not. The program never worked correctly, and that was one of many reasons why. (No strict, either.) Amazingly, this one dude had been working on it for six weeks before I saw it.
Re^2: Build your array with push
by PhilHibbs (Hermit) on Jan 31, 2006 at 16:15 UTC
    Something in me really doesn't like that trailing comma, which I wouldn't have expected to work. I suspect it's just an old habit, as it doesn't work in C & C++, which is still there lurking at the back of my mind and occasionally poisoning the Perl code that I write. OK, I'll try and get used to it, and thanks for introducing me to it - I trust that it really has no effect.

    Now to get back some of that lost XP by upvoting the helpful replies...
     ah, I like being a Friar anyway, it goes with my T-shirt.

      you don't have to merely trust; it's documented in perldata:
      You may have an optional comma before the closing parenthesis of a list literal
      In fact, you can have extra commas anywhere you want in a list except (for reasons I can't explain) at the very front of it. They will not cause undef items to be created, nor will including nested empty lists.
      my @items = ((),,,,5); print "<$_>\n" for @items; # prints only one item

      Caution: Contents may have been coded under pressure.

      Actually it does work in C, and that's most likely where perl picked up the habit too. One reason it's very useful is when you're generating code with macros, it's much easier to append commas to things than to insert them between things.

      --
      integral, resident of freenode's #perl
      
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: Build your array with push
by radiantmatrix (Parson) on Feb 01, 2006 at 16:35 UTC

    I use the former idiom as well, but I've run across this style a couple of times, too:

    my @array = ( 'Hibbs' ,'Daglish' ,'Schwartz' ,'Vroom' );

    The one time I had the chance to actually ask a coder about that style, he gave me two rationales. First, he liked it because of a string-cat convention that's similar:

    my $string = 'this is a very long string that has a certain number ' .$number .' of segments.' ."\n";

    In print statements, this coder often mirrored that convention using commas instead of periods to pass the list of strings to print. (This is apparently faster, though it smacks of premature optimization to me. But I digress.)

    The second reason was that he found the trailing comma to be confusing; by moving commas to the front, he provided a visual reminder to put the comma in when adding to the list but without the "confusing" trailing comma. To each their own, I guess.

    Anyone have thoughts on this style? Just curious as to what advantages/disadvantages there might be.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet

      That doesn't really help here since you can't insert at the top safely.

      The style has been called The Perlish Coding Style. Accoding to that document, the following would be the proper alignment:

      { my @array = ( 'Hibbs' , 'Daglish' , 'Schwartz' , 'Vroom' ) ; my $string = 'this is a very long string that has a certain number ' . $number . ' of segments.' . "\n" }