Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: Build your array with push

by PhilHibbs (Hermit)
on Jan 31, 2006 at 16:15 UTC ( #526784=note: print w/replies, xml ) Need Help??

in reply to Re: Build your array with push
in thread Build your array with push

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.

Replies are listed 'Best First'.
Re^3: Build your array with push
by Roy Johnson (Monsignor) on Jan 31, 2006 at 16:44 UTC
    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.
Re^3: Build your array with push
by integral (Hermit) on Jan 31, 2006 at 19:40 UTC

    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

      No, it does not.

      $ cat > a.c int a[] = (1,2,3,); $ gcc -c a.c a.c:1: error: syntax error before ')' token

      Not even in C++

      $ cat > a.cpp int a[] = (1,2,3,); $ gcc -c a.cpp a.cpp:1: error: expected primary-expression before ')' token

        It helps to remember that in C you need to use {} as brackets around a compound initialiser (something which is different from perl, where , has taken on a much greater role).

        thaum:/tmp bsmith$ cat >a.c int a[] = { 1, 2, 3, }; thaum:/tmp bsmith$ gcc -c a.c thaum:/tmp bsmith$

        integral, resident of freenode's #perl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://526784]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-05-27 02:08 GMT
Find Nodes?
    Voting Booth?