mock5c
asked on
Add columns to 2D array
I want to take certain columns from @ary1 and @ary2 and add them to the @master array. I can do this easily if I added as rows but I want to add as columns. What is a generic method for doing this? The columns need to be added one at a time.
@ary1 = (
[1,2,3,4],
[2,3,4,5],
[3,4,5,6],
[4,5,6,7]
);
@ary2 = (
[2,4],
[4,8],
[0,0],
[0,0]
);
# After adding columns 0 and 1 from @ary1 and column 1 from @ary2, this should be the result.
# We'll go ahead and assume that the number of rows of @ary1 and @ary2 are the same.
@master = (
[1,2,4],
[2,3,8],
[3,4,0],
[4,5,0]
);
@ary1 = (
[1,2,3,4],
[2,3,4,5],
[3,4,5,6],
[4,5,6,7]
);
@ary2 = (
[2,4],
[4,8],
[0,0],
[0,0]
);
# After adding columns 0 and 1 from @ary1 and column 1 from @ary2, this should be the result.
# We'll go ahead and assume that the number of rows of @ary1 and @ary2 are the same.
@master = (
[1,2,4],
[2,3,8],
[3,4,0],
[4,5,0]
);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@master = map{[@{$ary1[$_]}[@from1], @{$ary2[$_ ]}[@from2] ]}0..$#ary 1;