my @arr = qw ( apples oranges bananas pears berries apricots peaches );
my @arr_of_columns = unmerge_list_d( \@arr, 2 );
####
['apples', 'bananas','berries', 'prunes']
['oranges','pears', 'apricots']
##
##
my $arr_of_arr = unmerge_list_d( [ 1 .. 7 ], 3, 'goofy' );
##
##
[
[1, 4, 7]
[2, 5, 'goofy']
[3, 6, 'goofy']
]
##
##
sub unmerge_list_d {
my ( $list_ref, $qty, $pad ) = @_;
return unless ref $list_ref eq 'ARRAY';
my $arr;
push @$arr, [] for 1 .. $qty;
if ( @$list_ref % $qty && @_ > 2) {
my $padded = $qty - @$list_ref % $qty ;
push @{ $list_ref }, ($pad) x $padded;
}
LOOP:
while (1) {
foreach ( @$arr ) {
push @{ $_ }, shift @$list_ref;
last LOOP unless @$list_ref;
}
}
return @$arr if wantarray;
return $arr;
}