Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to print Perl array? (with a little formatting)

Tags:

perl

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.

like image 299
funk-shun Avatar asked Apr 21 '11 07:04

funk-shun


People also ask

How do I print an entire array in Perl?

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!

How do I print an array of elements in a new line in Perl?

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..


2 Answers

Just use join():

# assuming @array is your array: print join(", ", @array); 
like image 198
Alex Avatar answered Oct 08 '22 16:10

Alex


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 })" 
like image 41
Eugene Yarmash Avatar answered Oct 08 '22 17:10

Eugene Yarmash