106

Is there an easy way to print out a Perl array with commas in between each element?

Writing a for loop to do it is pretty easy but not quite elegant....if that makes sense.

codeforester
  • 39,467
  • 16
  • 112
  • 140
funk-shun
  • 4,331
  • 11
  • 32
  • 41

11 Answers11

160

Just use join():

# assuming @array is your array:
print join(", ", @array);
Alex
  • 64,178
  • 48
  • 151
  • 180
  • 9
    Or, if you want to be really dirty: `{local $,=', ';print @array}`. – musiKk Apr 21 '11 at 07:57
  • 10
    There's more than one way to do it.. but this is the way that doesn't make the person maintaining the code after you hate you. Yes, perldoc perlvar exists, but I'd rather glance over "join ', ' @array" than hit up perlvar every other line to figure out what all the esoteric variables are doing. – Oesor Apr 21 '11 at 14:17
  • @Oesor: That's why I called it dirty. But well, looking at the other answers, I'm not really the worst offender. :) – musiKk Apr 21 '11 at 20:03
  • @musiKK I like it! It's something I'd use if I wanted to puzzle my fellow co workers. :) – Alex Apr 21 '11 at 20:06
  • 4
    This worked really nicely for printing the directory contents. `print "current directory contains " . join(', ', <*>) . "\n";` – Randall Apr 04 '13 at 19:03
37

You can use Data::Dump:

use Data::Dump qw(dump);
my @a = (1, [2, 3], {4 => 5});
dump(@a);

Produces:

"(1, [2, 3], { 4 => 5 })"
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
20

If you're coding for the kind of clarity that would be understood by someone who is just starting out with Perl, the traditional this construct says what it means, with a high degree of clarity and legibility:

$string = join ', ', @array;
print "$string\n";

This construct is documented in perldoc -fjoin.

However, I've always liked how simple $, makes it. The special variable $" is for interpolation, and the special variable $, is for lists. Combine either one with dynamic scope-constraining 'local' to avoid having ripple effects throughout the script:

use 5.012_002;
use strict;
use warnings;

my @array = qw/ 1 2 3 4 5 /;

{
    local $" = ', ';
    print "@array\n"; # Interpolation.
}

OR with $,:

use feature q(say);
use strict;
use warnings;

my @array = qw/ 1 2 3 4 5 /;
{
    local $, = ', ';
    say @array; # List
}

The special variables $, and $" are documented in perlvar. The local keyword, and how it can be used to constrain the effects of altering a global punctuation variable's value is probably best described in perlsub.

Enjoy!

DavidO
  • 13,812
  • 3
  • 38
  • 66
10

Also, you may want to try Data::Dumper. Example:

use Data::Dumper;

# simple procedural interface
print Dumper($foo, $bar);
Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
Andreas
  • 2,211
  • 1
  • 18
  • 36
  • 4
    Data::Dumper is a standard module and is installed along with Perl. To get a list of all the standard pragmatics and modules, see `pelrdoc perlmodlib`. – shawnhcorey Apr 21 '11 at 13:25
7

For inspection/debugging check the Data::Printer module. It is meant to do one thing and one thing only:

display Perl variables and objects on screen, properly formatted (to be inspected by a human)

Example usage:

use Data::Printer;  
p @array;  # no need to pass references

The code above might output something like this (with colors!):

   [
       [0] "a",
       [1] "b",
       [2] undef,
       [3] "c",
   ]
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
4

You can simply print it.

@a = qw(abc def hij);

print "@a";

You will got:

abc def hij
Yi Zhao
  • 6,768
  • 1
  • 18
  • 18
  • 2
    You have commas in the print because they are in each element of the array. You really should declare `use warnings;` and see what it says. – Toto Apr 21 '11 at 08:29
2
# better than Dumper --you're ready for the WWW....

use JSON::XS;
print encode_json \@some_array
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
gleeco
  • 21
  • 1
1

Using Data::Dumper :

use strict;
use Data::Dumper;

my $GRANTstr = 'SELECT, INSERT, UPDATE, DELETE, LOCK TABLES, EXECUTE, TRIGGER';
$GRANTstr    =~ s/, /,/g;
my @GRANTs   = split /,/ , $GRANTstr;

print Dumper(@GRANTs) . "===\n\n";

print Dumper(\@GRANTs) . "===\n\n";

print Data::Dumper->Dump([\@GRANTs], [qw(GRANTs)]);

Generates three different output styles:

$VAR1 = 'SELECT';
$VAR2 = 'INSERT';
$VAR3 = 'UPDATE';
$VAR4 = 'DELETE';
$VAR5 = 'LOCK TABLES';
$VAR6 = 'EXECUTE';
$VAR7 = 'TRIGGER';
===

$VAR1 = [
          'SELECT',
          'INSERT',
          'UPDATE',
          'DELETE',
          'LOCK TABLES',
          'EXECUTE',
          'TRIGGER'
        ];
===

$GRANTs = [
            'SELECT',
            'INSERT',
            'UPDATE',
            'DELETE',
            'LOCK TABLES',
            'EXECUTE',
            'TRIGGER'
          ];
TVNshack
  • 107
  • 6
1

This might not be what you're looking for, but here's something I did for an assignment:

$" = ", ";
print "@ArrayName\n";
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
009
  • 83
  • 1
  • 1
  • 11
  • Where is this $" variable documented? Is there a name for it? Edit: found it here: https://perldoc.perl.org/variables – Roman Feb 02 '22 at 21:51
1

Map can also be used, but sometimes hard to read when you have lots of things going on.

map{ print "element $_\n" }   @array; 
PodTech.io
  • 4,874
  • 41
  • 24
1

I've not tried to run below, though. I think this's a tricky way.

map{print $_;} @array;
K.Minoda
  • 83
  • 1
  • 9