This looks a little like a problem I had laying out buttons in a Perl/Tk application using the "grid" method for packing them into rows and columns. After a lot of head scratching I wrote a small module that exported four routines that fit a number of items in a list to a number of either rows or columns sorting either horizontally or vertically. In this problem we'd fit to two columns sorting vertically. Here is the module; be gentle with me, I wrote it a long time ago:-)
# ====
package Grid;
# ====
use Carp;
use strict;
use integer;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
fitToColsVSort
fitToColsHSort
fitToRowsVSort
fitToRowsHSort);
# --------------
sub fitToColsVSort
# --------------
{
my($numItems, $numCols) = @_;
my $rlOrder = [];
my $leftOver = $numItems % $numCols;
my $maxRowNo = $numItems / $numCols + ($leftOver ? 1 : 0);
my $rowNo = 0;
my $colNo = 0;
for (1 .. $numItems)
{
push @$rlOrder, [$rowNo, $colNo];
$rowNo ++;
if($rowNo == $maxRowNo)
{
if($leftOver)
{
$leftOver --;
$maxRowNo -- unless $leftOver;
}
$rowNo = 0;
$colNo ++;
}
}
return $rlOrder;
}
# --------------
sub fitToColsHSort
# --------------
{
my($numItems, $numCols) = @_;
my $rlOrder = [];
foreach my $item (0 .. ($numItems - 1))
{
push @$rlOrder, [($item) / $numCols,
($item) % $numCols];
}
return $rlOrder;
}
# --------------
sub fitToRowsVSort
# --------------
{
my($numItems, $numRows) = @_;
my $rlOrder = [];
foreach my $item (0 .. ($numItems - 1))
{
push @$rlOrder, [($item) % $numRows,
($item) / $numRows];
}
return $rlOrder;
}
# --------------
sub fitToRowsHSort
# --------------
{
my($numItems, $numRows) = @_;
my $rlOrder = [];
my $leftOver = $numItems % $numRows;
my $maxColNo = $numItems / $numRows + ($leftOver ? 1 : 0);
my $rowNo = 0;
my $colNo = 0;
for (1 .. $numItems)
{
push @$rlOrder, [$rowNo, $colNo];
$colNo ++;
if($colNo == $maxColNo)
{
if($leftOver)
{
$leftOver --;
$maxColNo -- unless $leftOver;
}
$colNo = 0;
$rowNo ++;
}
}
return $rlOrder;
}
1;
and here is an example of it's use:-
...
$rlButtonOrder = fitToColsVSort($numHosts, $numCols);
foreach (0 .. $#nodeList)
{
my $buttonName = $nodeList[$_] . "Button";
$$buttonName = $hostSelectFrame->Button(
...
-text => $nodeList[$_],
...
-width => $longestName)->grid(
-row => $rlButtonOrder->[$_]->[0],
-column => $rlButtonOrder->[$_]->[1],
...
);
$$buttonName->configure(
-command => [\&gotoHost, $nodeList[$_]]);
}
...
In essence, what it does is take as arguments the number of items to be fitted and the number of rows or columns to fit them in; you choose the right routing for vertical or horizontal sorting to rows or columns. Returned is a LoL of row/column position for each element of the list to be output.
Although the problem is different the algoriths should hopefully still work, counting the keys, using fitToColsVSort(11, 2) to get row/column places then sorting your hash and displaying it.
In case you were wondering, the application I wrote it for was to fire up a terminal and open a telnet session to a host at the click of a button. Cheers, JohnGG |