Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

creating HTML table

by GertMT (Friar)
on Jul 16, 2007 at 08:13 UTC ( #626793=perlquestion: print w/ replies, xml ) Need Help??
GertMT has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,

I'm trying to create a HTML table that together with the use of javascript and css can be sorted. It's mentioned on the page: The data I'm putting in the table comes from a csv file. As I've not been able to create a table from csv with the HTML::template module I'm trying to create a table via the code that is mentioned below.

Would someone be able to tell me how I can make a difference between odd/even rows?

Or is there a much better way of doing this then via the code mentioned? ; (

Thanks for any reply,


#!/usr/bin/perl -w use strict; use diagnostics; open( OUT_kolom, ">tabel.html" ) or die "cant open out $!"; # my $file = "debug.csv"; # open FILE, '<', $file or warn "Can't open FILE: $!"; # real code print OUT_kolom "<table class=\"sort-table\" id=\"table-1\" cellspacing=\"0\">\n"; print OUT_kolom "\t<thead>\n"; print OUT_kolom "\t\t<tr>\n"; print OUT_kolom "\t\t\t<td>Number</td>\n"; print OUT_kolom "\t\t\t<td>Letter</td>\n"; print OUT_kolom "\t\t\t<td>Date</td>\n"; print OUT_kolom "\t\t</tr>\n"; print OUT_kolom "\t</thead>\n"; print OUT_kolom "\t<tbody>\n"; while (<DATA>) { chomp; print OUT_kolom "\t\t<tr>\n"; print OUT_kolom map { "\t\t\t<td>$_</td>\n" } split( /,/, $_ ); # how to get <tr class="odd"?> # how to get <tr class="even"?> print OUT_kolom "\t\t</tr>\n"; } print OUT_kolom "\t</tbody>\n"; print OUT_kolom "</table>\n"; close OUT_kolom; __DATA__ 1,X,2002-04-18, 2,Y,2004-05-06, 3,Z,2007-07-16,

Replies are listed 'Best First'.
Re: creating HTML table
by moritz (Cardinal) on Jul 16, 2007 at 08:42 UTC
    Maybe you should try to get HTML::Template or another templating system (like Template or HTML::Template::Compiled) to work, instead of reimplementing it in part.

    If you have a problem with the template system, tell us what it is ;)

    If you insist on implementing it as you did, you can either use a counter or $. to determine the line number of the input file handle, and then test with if ($. % 2 == 0) if the number is even.

    You could do something like:

    my $line = 1; while (<data>){ my $class = ($line % 2) == 0 ? "even" : "odd"; print qq{\t\t\t<tr class="$class">}; # rest of the loop here $line++; }
      my $class = $line & 1 ? "odd" : "even";

      or even

      my $class = $. & 1 ? "odd" : "even";

      and get rid of $line :-)


      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Thanks for all input. With the code mentioned below that is inspired by your input and some article re. HTML::template on I've got HTML::template creating a nice table with odd/even rows. Just one thing to work on is how if it were not a single column table. Guess I've got to read the csv that's providing the data for the table into a hash. That'll be a challenge again.
        #!/usr/bin/perl -w use strict; use diagnostics; use HTML::Template; open( OUT, ">fruit.html" ) or die "cant open out $!"; my $html = do{local $/;<DATA>}; my $tpl = HTML::Template->new( scalarref => \$html, loop_context_vars => 1, filter => \&cstmrow_filter ); my @rows = ( "Apple", "Orange", "Mango", "Kiwi" ); my @loop_data = (); # initialize an array to hold my loop while (@rows) { my %row_data; # a hash for my data list $row_data{FILES} = shift @rows; push( @loop_data, \%row_data ); } $tpl->param( rows => \@loop_data ); print OUT $tpl->output; sub cstmrow_filter { my $text_ref = shift; $$text_ref =~ s/<CSTM_ROW\s+EVEN=(.+)\s+ODD=(.*)\s*> /<TMPL_IF NAME=__odd__> <tr class="$1"> <TMPL_ELSE> <tr class="$2"> <\/TMPL_IF> /gx; } __DATA__ <head> <style type="text/css"> .odd { background-color: #d3d3d3 } .even { background-color: #90ee90 } </style> </head> <body> <p> <table class="center"> <TMPL_LOOP NAME=rows> <CSTM_ROW EVEN=even ODD=odd> <td> <TMPL_VAR NAME=FILES></td> </tr> </TMPL_LOOP> </table> </p> </body> </html>
      HTML table titorial...

        How is this applicable here?

Re: creating HTML table
by wfsp (Abbot) on Jul 16, 2007 at 09:21 UTC
    From the HTML::Template docs:
    <TMPL_LOOP NAME="FOO"> <TMPL_IF NAME="__first__"> This only outputs on the first pass. </TMPL_IF> <TMPL_IF NAME="__odd__"> This outputs every other pass, on the odd passes. </TMPL_IF> <TMPL_UNLESS NAME="__odd__"> This outputs every other pass, on the even passes. </TMPL_UNLESS> <TMPL_IF NAME="__inner__"> This outputs on passes that are neither first nor last. </TMPL_IF> This is pass number <TMPL_VAR NAME="__counter__">. <TMPL_IF NAME="__last__"> This only outputs on the last pass. </TMPL_IF> </TMPL_LOOP>
    I reckon that's just about everything you need to pretty up a table. :-)
Re: creating HTML table
by neniro (Priest) on Jul 16, 2007 at 11:22 UTC
    I'd like to suggest HTML::Table :
    #!/usr/bin/perl use strict; use warnings; use HTML::Table; my $data = [map { [ split /,/, $_ ] } (<DATA>)]; my $table = HTML::Table->new( -class => 'sortable', -evenrowclass => 'roweven', -oddrowclass => 'rowodd', -head => ['nr', 'id', 'date'], -data => $data, ); print $table->getTable; __DATA__ 1,X,2002-04-18 2,Y,2004-05-06 3,Z,2007-07-16
      this looks as straightforward that I can understand it! Never used this module but I'm starting with it now. Creating the html-table is (for me at least) much easier like this then with HTML::template. I can still easilly use the created table in my template via HTML::template.


Re: creating HTML table
by idsfa (Vicar) on Jul 16, 2007 at 14:00 UTC

    If you're going to be requiring the use of JavaScript anyway, here's an article which contains a script to do the striping for you. Why manually keep track of oven and odd rows?

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
      javascript is on my list. For now I'll use perl and js for the on-site sorting. I'll definitely look into it later!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://626793]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2016-07-25 17:50 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (225 votes). Check out past polls.