How can I highlight the last row of an HTML table generated using Perl?
The code below breaks up the @stat_array into fours and then prints out fields into an HTML table with the OCOMsg2 cell in red - again - just one cell of the emailed table.
The boss told me that he wants the WHOLE LINE with the OCOMsg2 in red - so the array below, all the elements
"ibfarm102 - localtick", 'New York", "hibmis100 - procHKHD2 - Hongkong", "OCOMsg2"
need to be red.
Is there a way to do this with out rewriting the whole block - I really like this block.
@stat_array = ("ibfarm102 - localtick", "Boston" , "hibmis100 - procHKHD2 - Hongkong", "PidMonRsp",
"eufarm102 - localtick", "London", "hibmis100 - procHKHD2 - Hongkong" , "PidMonReq" ,
"ibfarm102 - localtick", "New York" , "hibmis100 - procHKHD2 - Hongkong", "PidMonRsp",
"ibfarm102 - localtick", 'New York", "hibmis100 - procHKHD2 - Hongkong", "OCOMsg2");
my @stat_array_rows = @stat_array;
while (my @stat_array_rows = splice(@stat_array_rows, 0 , 4)) {
print MAIL "<tr>\n";while (my @stat_array_rows = splice(@stat_array_rows, 0 , 4)) {
print MAIL "<tr>\n";
for my $stat_row(@stat_array_rows) {
if ($stat_row =~ /OCCOMsg2/){
print MAIL "<td><font color=red>$stat_row[0]</font></td>\n";
}
else {
print for my $stat_row(@stat_array_rows) {
if ($stat_row =~/OCAlive2/){
print MAIL "<td><font color=red>$stat_row[0]</font></td>\n";
}
else {
print MAIL "<td>$stat_row</td>\n";
}
}
print MAIL "</tr>\n";
}
these are the results of the block:
<tr>
<td>ibfarm102 - localtick </td>
<td> Boston</td>
<td> hibmis100 - procHKHD2 - Hongkong </td>
<td>PidMonRsp</td>
开发者_StackOverflow</tr>
<tr>
<td>eufarm102 - localtick </td>
<td>London</td>
<td> hibmis100 - procHKHD2 - Hongkong </td>
<td>PidMonReq</td>
</tr>
<tr>
<td>ibfarm102 - localtick </td>
<td>New York</td>
<td> hibmis100 - procHKHD2 - Hongkong </td>
<td>PidMonRsp</td>
</tr>
<tr>
<td>ibfarm102 - localtick </td>
<td>New York</td>
<td> hibmis100 - procHKHD2 - Hongkong </td>
<td><font color=red> OCOMsg2</font></td>
</tr>
I somehow want Perl to print everything i the OCOMsg2 block red, not just that cell.
When you have the data in @stat_array_rows (a rather badly named variable in my opinion as it only contains a single row at a time) you just need to check if any of the elements contains 'OCOMsg2' and set a flag which you can use within the rest of the code.
my $is_OCOMsg2 = grep { /OCOMsg2/ } @stat_array_rows;
Also. This is 2011. People don't use the FONT tag any more. You should use CSS for this.
I tried to guess what you are about to do - your code as posted cannot be even compiled (things like else
after for
loop are pretty much invalid in perl).
What about something like this:
@stat_array = (
"ibfarm102 - localtick", "Boston", "hibmis100 - procHKHD2 - Hongkong", "PidMonRsp",
"eufarm102 - localtick", "London", "hibmis100 - procHKHD2 - Hongkong", "PidMonReq",
"ibfarm102 - localtick", "New York", "hibmis100 - procHKHD2 - Hongkong", "PidMonRsp",
"ibfarm102 - localtick", "New York", "hibmis100 - procHKHD2 - Hongkong", "OCOMsg2",
);
my @stat_array_rows = @stat_array;
while (my @cols = splice(@stat_array_rows, 0, 4)) {
my $color = $cols[3] eq "OCOMsg2";
print "<tr>\n ";
print join "\n ", map {
"<td>"
. ($color ? "<font color=red>" : "")
. $_
. ($color ? "</font>" : "")
. "</td>"
} @cols;
print "\n</tr>\n";
}
Probably better way to colorize would be use of CSS, though.
You'd better off using a template. For this purpose, I would go with HTML::Template. Coupled with List::MoreUtils::natatime, you can solve the problem cleanly.
#!/usr/bin/env perl
use warnings; use strict;
use HTML::Template;
use List::MoreUtils qw( natatime );
my $tmpl = HTML::Template->new(
scalarref => \ do {local $/; <DATA>}
);
my @stat_array = (
"ibfarm102 - localtick", "Boston" , "hibmis100 - procHKHD2 - Hongkong", "PidMonRsp",
"eufarm102 - localtick", "London", "hibmis100 - procHKHD2 - Hongkong" , "PidMonReq" ,
"ibfarm102 - localtick", "New York" , "hibmis100 - procHKHD2 - Hongkong", "PidMonRsp",
"ibfarm102 - localtick", "New York", "hibmis100 - procHKHD2 - Hongkong", "OCOMsg2"
);
my @rows;
my $it = natatime 4, @stat_array;
while (my @cells = $it->()) {
push @rows, {
CELLS => [map +{CELL => $_}, @cells],
HIGHLIGHT => $cells[-1] eq 'OCOMsg2'
};
}
$tmpl->param(ROWS => \@rows);
print $tmpl->output;
__DATA__
<!DOCTYPE HTML>
<html>
<body>
<table>
<TMPL_LOOP ROWS>
<TMPL_IF HIGHLIGHT>
<tr style="color:red">
<TMPL_ELSE>
<tr>
</TMPL_IF>
<TMPL_LOOP CELLS>
<td><TMPL_VAR CELL></td>
</TMPL_LOOP>
</tr>
</TMPL_LOOP>
</table>
</body>
</html>
精彩评论