pairwise from List::MoreUtils requires that both arrays have the same length, you can get the same behaviour easily without using any extra module at all:
my @combined = map {"$a1[$_]:$a2[$_]"} ($#a1 < $#a2 ? (0..$#a2) : (0..
+$#a1));
This version can be easily fixed to treat arrays of different length:
my @combined = map {($a1[$_]//"").":".($a2[$_]//"")} ($#a1 < $#a2 ? (0
+..$#a2) : (0..$#a1));
| [reply] [d/l] [select] |
The given example uses arrays of the same length, so pairwise looks fine to me.
And if they are not of the same length, I think the same way as JavaFan, that iswas left as an exercise to the reader. I don'tdidn't want to guess, what has to happen if the lengths differ. It's the OP who should know, what to do.
Nevertheless, thanks for your examples ;o)
Update:
What about this pairwise solution to work with arrays of different lengths:
use List::MoreUtils qw( pairwise );
my @a=(1 .. 5 );
my @b=( 'a' .. 'c' );
### Good point from JavaFan; bad idea to ignore 0
### my @combined = pairwise { ( $a || '' ) .':'. ( $b || '' ) } @a, @b
+;
### fixed:
my @combined = pairwise { ( defined $a ? $a : '' ) .':'. ( defined $b
+? $b : '' ) } @a, @b;
{
local $,="\n",
print @combined;
}
| [reply] [d/l] [select] |
I think it's a bad idea to replace a 0 with an empty string as your solution does. Either use defined, '//', or just turn off warnings.
| [reply] [d/l] |