1

I need to create a 2 tables in HTML format. Each has 5 rows:

1st Table

  • 1st row has FRUITS in it, occupying all columns
  • 2nd row has January(month), occupying all columns
  • 3rd row has names of some 6 fruits (apple, orange, grapes,...)These names do not change. so this row has 6 columns
  • 4th row has rates for each fruit ( 10,20,30..) so this has 6 columns.
  • 5th row has corresponding message for each fruit showing as Available or not.

2nd Table

If it is available the background color for the cell should be green and if not RED.

  • 1st row has VEGETABLES in it, occupying all columns
  • 2nd row has February(month), occupying all columns
  • 3rd row has names of some 6 vegetables (tomato, potato..)These names do not change. so this row has 6 columns
  • 4th row has rates for each vegetable ( 10,20,30..) so this has 6 columns.
  • 5th row has corresponding message for each vegetable showing as Available or not.If it is available the background color for the cell should be green and if not RED.

All this data is read from a file having a particular format, it is

<name of fruit/vegetable> price <available or not>

The names of fruits and vegetable do not change , it will be same for both the tables. However, it might be possible that data for a particular fruit/vegetable is not present. if it is not present the column for that should show N/A with white background.

I cannot use MIME:Lite for this. Need to use print <<ENDHTML;

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
anon
  • 11
  • 1
  • 1
  • 2
  • 4
    If this is homework, please label it as such. – Timo Mar 19 '10 at 11:59
  • 1
    Where would `MIME::Lite` even come in to the picture? Anyway, http://catb.org/~esr/faqs/smart-questions.html – Sinan Ünür Mar 19 '10 at 13:09
  • What have you tried so far? There must be zillions of examples of this in Google. Searching for "html table perl" has this as the third hit: http://osstips.wordpress.com/2005/12/13/perl-modules-to-generate-html-table/ – brian d foy Mar 19 '10 at 16:17

1 Answers1

1
use HTML::TagTree;
use strict;

my $html = HTML::TagTree->new('html');
$html->head->title('Fruits');

my $table = $html->body->table();
$table->tr->td('Fruits','colspan=6');
$table->tr->td('February','colspan=6');
my @fruits = qw(apples bananas coconut dates figs guava);
my @rates = (10,20,30,40,50,60);
my $tr_fruits = $table->tr;
my $tr_rates = $table->tr;
my $tr_available = $table->tr;
for (my $col=0; $col<6; $col++) {
   $tr_fruits->td($fruits[$col]);
   $tr_rates->td($rates[$col]);
   # Randomly make available
   my $available = int(rand(1000)/500 + .5);
   if ($available) {
      $tr_available->td('Available','style=background-color:green');
   } else {
      $tr_available->td('Out of Stock','style=background-color:red');
   }
}
print $html->print_html();
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98