in reply to how to print two array with new line at end of each second array element, using a single print statement?
Here's a simple way:
Here's an alternative way using the List::MoreUtils CPAN module:my @nums = (1, 2, 4, 5, 8, 9); my @dat = (3, 9, 14, 10, 12, 8); for my $i ( 0 .. $#nums ) { print "$nums[$i] $dat[$i]\n"; }
Update: Simpler solution using the pairwise function:use List::MoreUtils qw(zip natatime); my @nums = (1, 2, 4, 5, 8, 9); my @dat = (3, 9, 14, 10, 12, 8); my $it = natatime( 2, zip(@nums, @dat) ); while ( my @vals = $it->() ) { print "@vals\n"; }
use List::MoreUtils qw(pairwise); my @nums = (1, 2, 4, 5, 8, 9); my @dat = (3, 9, 14, 10, 12, 8); print pairwise { "$a $b\n" } @nums, @dat;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to print two array with new line at end of each second array element, using a single print statement?
by davido (Cardinal) on Apr 21, 2012 at 06:48 UTC |