How can I print N array elements with delimiters per line?
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/26
4:33 UTC
Read the original article
Hit count: 194
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