# Takes a number of columns then a list. Reformats the list into
# an array of arrays, with the original elements sorted down by
# columns, and any missing items missing off of the last column
sub reformat_major_minor {
my $dim = shift;
my $row_size = int((@_+$dim - 1)/$dim);
my @ret;
foreach my $i (0..$#_) {
$ret[$i%$row_size][$i/$row_size] = $_[$i];
}
return @ret;
}
####
foreach my $row (reformat_major_minor(3, @cells)) {
print "\n", (map " | $_ | \n", @$row), "
\n";
}
####
# Takes a number of columns then a list. Reformats the list into
# an array of arrays, with the original elements sorted down by
# columns, and any missing items missing off of the last column.
sub reformat_major_minor {
my $dim = shift;
my $row_size = int(@_/$dim);
my @ret;
push @ret, [splice @_, 0, $row_size] while @_;
return swap_dims(@ret);
}
# Takes an array of arrays and swaps rows for columns.
sub swap_dims {
my @ret;
foreach my $row_ind (0..$#_) {
my $row = $_[$row_ind];
foreach my $col_ind (0..$#$row) {
$ret[$col_ind][$row_ind] = $row->[$col_ind];
}
}
return @ret;
}