HTML::Table is a very handy module that gives you a table object which you can manipulate for a while before finally printing it out.

The rows and columns in a HTML::Table are counted on a grid, starting at 1,1. Cells that span several columns and/or rows do not disturb the count:

+---+---+---+---+---+
|1/1|2/1|3/1|4/1|5/1|
+---+---+---+---+---+
|1/2|2/2|3/2|4/2|5/2|
+   +---+---+---+---+
|   |2/3|3/3    |5/3|
+---+---+---+---+---+
|1/4|2/4|3/4|4/4    |
+---+---+---+       +
|1/5|2/5|3/5|       |
+---+---+---+---+---+

Here's my test-data:

csc,tech,base csc,comp,acm csc,mous,base new,this,that other,some,sing other,some,sels

And the program:

use SuperSplit; use HTML::Table; $data = supersplit_open( ',', 'table.txt');
I first generate an array that holds the first column, in my example that's (csc,csc,csc,new,other,other). From that I compute a seconde array that hold the count of the words in the first column, here this is (3,0,0,1,2,0). (I bet someone here could turn this into a one-liner <kbd>;-)</kbd>
@firstcol = map { $$_[0] } @$data; @numbers = (1) x scalar(@firstcol); # init to (1,1,1,...) for ($i = $#firstcol; $i > 0; $i--) { if ($firstcol[$i] eq $firstcol[$i-1]) { $numbers[$i-1] = $numbers[$i]+1; $numbers[$i] = 0; } }
Then I generate the plain table (without spanning cells)
$table = new HTML::Table; $table -> setBorder(2); foreach $row (@$data) { $table->addRow( @$row ); } # $table->print;
And finally I walk down the first column and set the spanning:
foreach $rowno (0..$#firstcol) { if ($numbers[$rowno]) { $table->setCellRowSpan($rowno+1, 1, $numbers[$rowno]); $table->setRowVAlign($rowno+1, "TOP") }; } $table->print;

The output of the program:
csctechbase
compacm
mousbase
newthisthat
othersomesing
somesels

Links and typos fixed june 11, thanks to jeffa

--
Brigitte    'I never met a chocolate I didnt like'    Jellinek
http://www.horus.com/~bjelli/         http://perlwelt.horus.at

In reply to Re: printing a table in html using data read from a text file by bjelli
in thread printing a table in html using data read from a text file by videogamer06

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.