Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by Anonymous Monk
on Aug 30, 2000 at 14:55 UTC ( [id://30279]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

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

Originally posted as a Categorized Question.

  • Comment on How do I print an array with commas separating each element?

Replies are listed 'Best First'.
Re: How do I print an array with commas separating each element?
by Jouke (Curate) on Aug 30, 2000 at 15:09 UTC
Re: How do I print an array with commas separating each element?
by ar0n (Priest) on Aug 30, 2000 at 15:13 UTC
    $, = ','; print @array;

    $, is the output field separator: print will insert whatever string has been assigned to $, between all of its arguments.

    Note that if you use this technique, you should make your assignment to $, temporary by doing

    local $, = ',';

Re: How do I print an array with commas separating each element?
by OfficeLinebacker (Chaplain) on Nov 04, 2006 at 19:25 UTC

    Observe the difference in effects between $" and $,

    $\ = "\n"; my @a = qw( One Two Three ); { local $, = ','; print @a; print "@a"; } { local $" = ','; print @a; print "@a"; }
    Output:
    One,Two,Three One Two Three OneTwoThree One,Two,Three

Re: How do I print an array with commas separating each element?
by Roy Johnson (Monsignor) on Oct 29, 2003 at 20:41 UTC

    $" can also be used for this:

    local $" = ','; print "@arr\n";
      Should titles in answers be checked for spelling?

      Separate has a rat in it.

      Update: Of course, misspelling it may make it easier to search for.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: How do I print an array with commas separating each element?
by nite_man (Deacon) on Feb 13, 2003 at 09:52 UTC
    And another way:
    map { print $_ . ($_ == $array[$#array] ? '' : ', ' ) } @array;
Re: How do I print an array with commas separating each element?
by Coruscate (Sexton) on Feb 13, 2003 at 20:36 UTC

    For completeness, here's a verbose solution:

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

      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)

Re: How do I print an array with commas separating each element?
by turnstep (Parson) on Aug 30, 2000 at 18:33 UTC

    Here's another way:

    my $x = 0; printf "$array[$x-1]%s", $x <= $#array ? "," : "\n" while $array[$x++];

    It puts a newline instead of a comma after the last element.

Re: How do I print an array with commas seperating each element?
by Nooks (Monk) on Aug 31, 2000 at 16:26 UTC

    Re: How do I print an array with commas seperating each element?

    Originally posted as a Categorized Answer.

Re: How do I print an array with commas seperating each element?
by Nooks (Monk) on Aug 31, 2000 at 16:27 UTC

    Re: How do I print an array with commas seperating each element?

    Originally posted as a Categorized Answer.

Re: How do I print an array with commas separating each element?
by hazem (Initiate) on Aug 30, 2015 at 17:38 UTC

    I don't understand how this works?

    How does this work?

    <p> local $, = ','; </p> <p> local $" = ',';</p>
    sub printSpecial() { $\ = "\n"; my @a = qw( One Two Three ); print ("\n"); print ("Join print statement\n"); print ("\n"); print join( ',', @a ); { # $, The output field separator for the print operator. local $, = ','; print @a; print "@a"; } { print ("\n"); local $" = ','; print @a; print "@a"; } }

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://30279]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-07-17 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.