in reply to creating HTML table

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++; }

Replies are listed 'Best First'.
Re^2: creating HTML table
by shmem (Chancellor) on Jul 16, 2007 at 09:06 UTC
    my $class = $line & 1 ? "odd" : "even";

    or even

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

    and get rid of $line :-)

    --shmem

    _($_=" "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 perl.com 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>
        You are making things really really difficult which HTML::Template can make simple.

        You don't need any of that <CSTM> nonsense, or the filter callback.

        Here's a template which does all you want:

        <table> <tmpl_loop name="rows"> <tr <tmpl_if name="__odd__"> class="odd" <tmpl_else> class="even" </tmpl_if> > <td> <tmpl_var name="data"> </td> </tr> </tmpl_loop> </table>

        You put the odd-even logic inside the opening TR tag and HTML::Template does the rest. The source code's going to look a bit weird but the browser doesn't care.



        Nobody says perl looks like line-noise any more
        kids today don't know what line-noise IS ...
        Hi.

        The code
        while (@rows) { my %row_data; # a hash for my data list $row_data{FILES} = shift @rows; push( @loop_data, \%row_data ); }
        is needlessly destructive to @rows. It can be simplified to
        for my $row (@rows) { push( @loop_data, {FILES => $row} ); }
        or
        for (@rows) { push( @loop_data, {FILES => $_} ); }
        or even
        @loop_data = map { {FILES => $_) } @rows;


        holli, /regexed monk/
        When you say you read an article on perl.com I assume it was this one. I thought that article was very strange, and it seemed to miss the point of HTML::Template entirely.

        It starts from the premise that if you have to use conditionals in HTML::Template, you'll be tearing your hair out, which I've never found to be the case, and it goes on to propose over-complicated solutions which spoil the simplicity, no matter how long-winded, of HTML::Template's approach, and create obscured code which would make it hard for an HTML coder and a Perl coder to work together, one of the key advantages of any good templating system.

        Throw in the fact that the someone writing an "Advanced" perl article on perl.com doesn't even know that you can use delimiters other than /// for regexes and the whole effect is decidedly strange.



        Nobody says perl looks like line-noise any more
        kids today don't know what line-noise IS ...
Re^2: creating HTML table
by justinhimt (Initiate) on Apr 27, 2014 at 08:47 UTC
    HTML table titorial... http://www.corelangs.com/html/tables/default.html

      How is this applicable here?