Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Answer: How do I print an array with commas separating each element?

by Coruscate (Sexton)
on Feb 13, 2003 at 20:36 UTC ( #235098=categorized answer: print w/replies, xml ) Need Help??

Q&A > arrays > How do I print an array with commas separating each element? - Answer contributed by Coruscate

For completeness, here's a verbose solution:

my $x; my @a = qw(one two three); print "$a[$_-1], " while $x++ < $#a; print "$a[$#a]";

  • Comment on Answer: How do I print an array with commas separating each element?
  • Download Code
Replies are listed 'Best First'.
Re: Answer: How do I print an array with commas seperating each element?
by extremely (Priest) on Feb 14, 2003 at 18:26 UTC

    Grr., I'd forgotten you can't reply to answers and have it be visible on the answer screen... Plus, this is only a mild (much shorter, tho) variation of another answer. *sigh* We're doing ridiculous ways now? Then why not do this?

    my $x; my @a = qw(one two three); print $_, ++$x<@a ? ", " : "\n" foreach @a;

    For bonus fun, you can crush all syntactical whitespace out of that print line, too.

    Still, I'm thinking I'll stick with the built-in join() for future projects...

    --
    $you = new YOU;
    honk() if $you->love(perl)

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2021-01-26 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?