Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Perl: the Markov chain saw
 
PerlMonks  

Re: printing a table in html using data read from a text file

by bjelli (Pilgrim)
on Jun 08, 2001 at 14:04 UTC ( #86871=note: print w/ replies, xml ) Need Help??


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:

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


Comment on Re: printing a table in html using data read from a text file
Select or Download Code
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
    Thanks! I took your code as an inspiration, and coded it for my next Web Techniques column, and will credit you in the column. Thanks for the idea!

    To play with the program, click on the column headers once for ascending sort, twice for descending. It autodetects whether there should be an alpha sort or a numeric sort. Yeah, it's not the best, but the program's only 70 lines of code or so.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://86871]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-04-21 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls