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


In reply to Re: split hash into two columns by johngg
in thread split hash into two columns by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.