in reply to printing a table in html using data read from a text file
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:
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>use SuperSplit; use HTML::Table; $data = supersplit_open( ',', 'table.txt');
Then I generate the plain table (without spanning cells)@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; } }
And finally I walk down the first column and set the spanning:$table = new HTML::Table; $table -> setBorder(2); foreach $row (@$data) { $table->addRow( @$row ); } # $table->print;
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:
csc | tech | base |
comp | acm | |
mous | base | |
new | this | that |
other | some | sing |
some | sels |
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
Replies are listed 'Best First'. | |
---|---|
Re: Re: printing a table in html using data read from a text file
by merlyn (Sage) on Jun 10, 2001 at 01:06 UTC |
In Section
Seekers of Perl Wisdom