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....
Just use join():
# assuming @array is your array:
print join(", ", @array);
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'
];
This might not be what you're looking for, but here's something I did for an assignment:
$" = ", ";
print "@ArrayName\n";
I've not tried to run below, though. I think this's a tricky way.
map{print $_;} @array;
Also, you may want to try Data::Dumper. Example:
use Data::Dumper;
# simple procedural interface
print Dumper($foo, $bar);
# better than Dumper --you're ready for the WWW....
use JSON::XS;
print encode_json \@some_array