This is sort of a continuation of question I previously asked, which was:

http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_21857321.html
Please see the comments after the accepted answer posting.

An Expert provided this code:

for( \@ary1, \@ary2, \@ary3 ){

my $right=@{$_->[0]};

my $left=@combinedary&&@{$com

binedary[0]};

push @$_,(0)x$right for @combinedary;

push @combinedary,[(0)x$left, @$_] for @$_;

}

The idea is to combined multiple arrays and create a "diagonal" of values and fill with zeros everywhere else, which is exactly what the above code accomplishes. But for this question, I would like to retain the first n columns. Please see below for a detailed description. This example only combines two arrays but the solution should apply to two or more arrays.

@ary1 = (

[1,2,3,4]

);

@ary2 = (

[10,9,8]

);

If n==2, then the final array would be:

@final = (

[1,2,3,4,0],

[10,9,0,0,8]

),

if n==1, then the final array would be:

@final = (

[1,2,3,4,0,0],

[10,0,0,0,9,8]

);

Another way to think of this would be to add @ary1 and @ary2:

@combined = (

[1,2,3,4],

[10,9,8]

);

Then split after n columns:

n==2:

@left = (

[1,2],

[10,9]

);

@right = (

[3,4],

[8]

);

Apply the above zero padding method to @right but leave @left alone:

@right_result = (

[3,4,0],

[0,0,8]

);

Finally, put @left and @right_result together:

@rightleft = (

[1,2,3,4,0]

[10,9,0,0,8]

);