I have written a Perl program to create a web page with an HTML table derived from text file textfile.txt
.
I would like to change it so that cells of the table are coloured according to the text content. For instance, if the text is Reject
then background of the cell should be red.
Here are two methods that I tried. Neither of them worked
if ( $_ eq "REJECT" ) {
print map { "<td style=width:705 bgcolor=#FF0000 >REJECT</td>" } @$d;
}
foreach my $d ( @data ) {
$d //= ''; # Convert undefined values to empty strings
my $class;
if ( $d eq 'REJECT' ) {
$class = 'hilite';
}
$html .= '<td';
$html .= " class='$class'" if $class;
$html .= ">$d</td>";
}
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use strict;
use warnings;
my $output = `cat textfile.txt`;
my @lines = split /\n/, $output;
my @data;
foreach my $line ( @lines ) {
chomp $line;
my @d = split /\s+/, $line;
push @data, \@d;
}
my $color1 = "black";
my $color2 = "darkgreen";
my $color3 = "black";
my $color4 = "red";
my $color5 = "lime";
my $num = 6;
my $title = "This is the heading";
my $fstyle = "Helvetica";
print "<body bgcolor = $color3>";
print "<font color = $color5 face = $fstyle size = $num>$title</font><br />";
foreach my $d ( @data ) {
print "<html>";
print "<body>";
print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>";
print "<tr>";
print map {"<th style=width:705 >Column1</th>"}
print map {"<th style=width:705 >Column2</th>"}
print "</tr>";
print "<tr>";
print map {"<td style=width:705 >$_</td>"} @$d;
if ( $d eq 'REJECT' ) {
print map {"<td style=width:705 bgcolor=#FF0000 >Reject</td>"} @$d;
}
print "</tr>";
print "</table>";
print "</body>";
print "</html>";
}
Input text file:
Column1 Column2
Accept Reject
Accept Reject
Accept Reject
This line
print map { "<td style=width:705 bgcolor=#FF0000 >Reject</td>"
is adding the background color RED to the cell but it is not matching the condition Reject
.
Here are some of the errors in your Perl code
As I said, you are misusing map
You are creating a new HTML document for every element of @data
. What do you expect the browser to do with multiple <html>
elements? It can't display them all
You are expecting the string REJECT
to match Reject
You are using a mixture of CSS style
strings and element attributes. For instance
print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>"
should be
print qq{<table style="table-layout:fixed; width=705; height=110; text=$color4" border=2 bordercolor="$color1" bgcolor="$color2">\\n}
because table-layout
, width
, height
, and text
are CSS properties, while border
, bordercolor
, and bgcolor
are HTML element attributes
I think you should be writing CSS to solve this problem, but that is another matter
It would also help you a lot if you printed a newline "\\n"
after each HTML element. That way the output will be much more readable and you will be able to see better what you have created
Please don't persist with this "try things until it works" approach. You always end up coming here for help to get you out of the mess you've created, and you're not asking intelligent questions. To be using map
like that after so long means you're not learning at all, and you owe it to yourself as well as your employer to learn the language properly
Here is a solution that performs correctly, but it is no more than a correction of your own code. The problems that I have outlined have been fixed, and that is all
#!/usr/bin/perl
use strict;
use warnings 'all';
my $color1 = 'black';
my $color2 = 'darkgreen';
my $color3 = 'black';
my $color4 = 'red';
my $color5 = 'lime';
my $size = 6;
my $title = 'This is the heading';
my $fstyle = 'Helvetica';
print "Content-type: text/html\n\n";
print "<body bgcolor = $color3>\n";
print "<font color = $color5 face=$fstyle size=$size>$title</font><br />\n";
{
print "<html>\n";
print "<body>\n";
print qq{<table
style="table-layout:fixed; width=705; height=110; text=$color4"
border=2
bordercolor="$color1"
bgcolor="$color2">\n};
print "<tr>\n";
print qq{<th style="width:705" >Column1</th>};
print qq{<th style="width:705" >Column2</th>};
print "</tr>\n";
open my $fh, '<', 'textfile.txt' or die $!;
while ( <$fh> ) {
my @line = split;
print "<tr>\n";
for ( @line ) {
if ( /reject/i ) {
print qq{<td style=width:705 bgcolor=red>$_</td>};
}
else {
print "<td style=width:705>$_</td>"
}
}
print "</tr>\n";
}
print "</table>\n";
print "</body>\n";
print "</html>\n";
}
Content-type: text/html
<body bgcolor = black>
<font color = lime face=Helvetica size=6>This is the heading</font><br />
<html>
<body>
<table
style="table-layout:fixed; width=705; height=110; text=red"
border=2
bordercolor="black"
bgcolor="darkgreen">
<tr>
<th style="width:705" >Column1</th><th style="width:705" >Column2</th></tr>
<tr>
<td style=width:705>Column1</td><td style=width:705>Column2</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
</table>
</body>
</html>
I still have misgivings about your approach. Hacking together a program from bits and pieces of others' work that you don't understand is a recipe for failure. If you have no inclination to explore and learn the details enough to survive on your own then you have chosen the wrong job
I think you should be using a template system such as Template::Toolkit
instead of printing HTML
from your Perl program
The colours should be changed using CSS and an appropriate class
, rather than printing HTML attributes in line
You seem to think that a casual and approximate approach is fine, or at least that you are unwilling to offer any more, but while that may be true of other professions, software engineering requires much more care and precision
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.