What‽ No love for jeffa's new Spreadsheet::HTML

#!/usr/bin/perl -w use strict; use Spreadsheet::HTML; my %hash = ( kumquat => 'orange', pomegranate => 'red', cherimoya => 'green', ling +onberry => 'purple' ); my $data = [ [sort keys %hash], [sort { $a cmp $b } values %hash] ]; my $generator = Spreadsheet::HTML->new( data => $data, matrix => 1, # no <th> indent => ' ' ); print $generator->portrait; __END__

Output:

<table> <tr> <td>cherimoya</td> <td>kumquat</td> <td>lingonberry</td> <td>pomegranate</td> </tr> <tr> <td>green</td> <td>orange</td> <td>purple</td> <td>red</td> </tr> </table>

Now if there was just a way to make a callback sub that would color the table cells with the fruit colors ...

#!/usr/bin/perl -w use strict; use Spreadsheet::HTML; my %hash = ( kumquat => 'orange', pomegranate => 'red', cherimoya => 'green', lin +gonberry => 'purple' ); my ($fruits, $colors) = ( [sort keys %hash], [sort { $a cmp $b } value +s %hash] ); my $data = [ $fruits, $colors ]; my $generator = Spreadsheet::HTML->new( data => $data, indent => ' ', th => { style => { background => $colors } }, td => [ sub { my $c = shift; $c =~ s!(.*)!<font color="$1">$1</f +ont>!; return $c } ], ); print $generator->portrait; __END__

Output:

<table> <tr> <th style="background: green">cherimoya</th> <th style="background: orange">kumquat</th> <th style="background: purple">lingonberry</th> <th style="background: red">pomegranate</th> </tr> <tr> <td><font color="green">green</font></td> <td><font color="orange">orange</font></td> <td><font color="purple">purple</font></td> <td><font color="red">red</font></td> </tr> </table>

Yep, I know this is deprecated HTML; just playing :-)

The way forward always starts with a minimal test.

In reply to Re: Table in Perl CGI by 1nickt
in thread Table in Perl CGI by alokranjan

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.