Update
I hope it's clear how to generate a 2D array given the same function. Here it is just in case
use strict;
use warnings;
use List::MoreUtils qw/ each_array /;
my @col1 = qw/ A B C D /;
my @col2 = qw/ 2 4 6 8 /;
my @col3 = qw/ Abc Ghy Tgh Yth /;
my $iter = each_array @col1, @col2, @col3;
my @result;
while ( my @row = $iter->() ) {
push @result, \@row;
}
use Data::Dump;
dd \@result;
output
[
["A", 2, "Abc"],
["B", 4, "Ghy"],
["C", 6, "Tgh"],
["D", 8, "Yth"],
]
Or if you'd rather do it without using a non-core module then this will do as you ask
use strict;
use warnings;
use List::Util qw/ max /;
my @col1 = qw/ A B C D /;
my @col2 = qw/ 2 4 6 8 /;
my @col3 = qw/ Abc Ghy Tgh Yth /;
my @cols = \(@col1, @col2, @col3);
my @result;
for my $i ( 0 .. max map $#$_, @cols ) {
push @result, [ map $_->[$i], @cols ];
}
The resulting @result is identical
Original post
I suggest that you make use of the each_array function from List::MoreUtils. Given a list of arrays, it returns an iterator function which will hand back the next set of values from those arrays each time it is called
Here is some example code that uses your own data
use strict;
use warnings;
use List::MoreUtils qw/ each_array /;
my @col1 = qw/ A B C D /;
my @col2 = qw/ 2 4 6 8 /;
my @col3 = qw/ Abc Ghy Tgh Yth /;
my $iter = each_array @col1, @col2, @col3;
while ( my @row = $iter->() ) {
print "@row\n";
}
output
A 2 Abc
B 4 Ghy
C 6 Tgh
D 8 Yth
@c:perl -E '@a=(1,2,3); @b=(4,5,6); for(0..$#a) {push @c, [$a[$_], $b[$_]]} for (@c) {say join " ", @$_}'