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.
This construct is documented in perldoc -f join . use 5.012_002; use strict; use warnings; my @array = qw/ 1 2 3 4 5 /; { local $" = ', '; print "@array\n"; # Interpolation. } Enjoy!
When you specify a newline character in this context, make sure to surround it with double quotes, not single quotes, or a backslash and an ``n'' will be used to join the pieces of the array. Thus, to print the numbers from 1 to 10, each on a single line, you could use the following perl statement: print join("\n",1..
Just use join()
:
# assuming @array is your array: print join(", ", @array);
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 })"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With