Printing array with delimiters in Perl
Posted
by Mark B
on Stack Overflow
See other posts from Stack Overflow
or by Mark B
Published on 2010-03-25T19:02:15Z
Indexed on
2010/03/25
19:23 UTC
Read the original article
Hit count: 318
perl
I have an array in Perl I want to print with space delimiters between each element, except every 10th element which should be newline delimited. There aren't any spaces in the elements if that matters.
I've written a function to do it with for and a counter, but I wondered if there's a better/shorter/canonical Perl way, perhaps a special join syntax or similar.
My function to illustrate:
sub PrintArrayWithNewlines
{
my $counter = 0;
my $newlineIndex = shift @_;
foreach my $item (@_)
{
++$counter;
print "$item";
if($counter == $newlineIndex)
{
$counter = 0;
print "\n";
}
else
{
print " ";
}
}
}
© Stack Overflow or respective owner