Loop through Multiple Arrays in Perl at the Same Time

Say you have more than one array that you want to loop through at the same time, pulling out the same row index from each per cycle through the loop. One way would need to loop over each index number:

for my $I (0 .. $#arrayA) {

   my $firstVal  = $arrayA[$I];
   my $secondVal = $arrayB[$I];


Another, perhaps more readable, option is to use each_array from List::MoreUtils.

use List::MoreUtils qw( each_array );

my @names = qw( John David Bob );
my @ages  = ( 30, 28, 36 );

my $row = each_array( @names, @age );
while ( my ($name, $age) = $row->() ) {
    print "$name is $age years old\n";

As with anything in Perl there's always another way.