There is a question very similar to this already but I would like to do this for multiple arrays. I have an array of arrays.
my @AoA = (
$arr1 = [ 1, 0, 0, 0, 1 ],
$arr2 = [ 1, 1, 0, 1, 1 ],
$arr3 = [ 2, 0, 2, 1, 0 ]
);
I want to sum the items of all the three (or more) arrays to get a new one like
( 4, 1, 2, 2, 2 )
The use List::MoreUtils qw/pairwise/ requires two array arguments.
@new_array = pairwise { $a + $b } @$arr1, @$arr2;
One solution that comes to mind is to loop through @AoA and pass the first two arrays into the pairwise function. In the subsequent iterations, I will pass the next @$arr in @AoA and the @new_array into the pairwise function. In the case of an odd sized array of arrays, after I've passed in the last @$arr in @AoA, I will pass in an equal sized array with elements of 0's.
Is this a good approach? And if so, how do I implement this? thanks