Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

perl generated table - format color

by casperdaghost (Initiate)
on Jul 26, 2011 at 08:31 UTC ( #916701=perlquestion: print w/ replies, xml ) Need Help??
casperdaghost has asked for the wisdom of the Perl Monks concerning the following question:

Oh fellow seekers of wisdom..We shape clay into a pot, but it is the emptiness inside that holds whatever we want.....
The code below breaks up the @stat_array into fours and then prints out fields in to an HTML table wih the OCOMsg2 cell in red - again - just one cell of the emailed table. print Dumper @stat_array_rows; sleep 1; exit; 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. print Dumper @stat_array_rows; sleep 1; exit; Is there a way to do this with out rewriting the whole block - I really like this block..

@stat_array = ("ibfarm102 - localtick", "Boston" , "hibmis100 - proc +HKHD2 - Hongkong", "PidMonRsp", "eufarm102 - localtick", "London", "hibmis100 - procHKHD2 - Hongkong +" , "PidMonReq" , "ibfarm102 - localtick", "New York" , "hibmis100 - procHKHD2 - Hongk +ong", "PidMonRsp", "ibfarm102 - localtick", 'New York", "hibmis100 - procHKHD2 - Hongko +ng", "OCOMsg2"); my @stat_array_rows = @stat_array; 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 eq $config_msgtype){ 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"; }

this are the results of the block:

<tr> <td>ibfarm102 - localtick </td> <td> Boston</td> <td> hibmis100 - procHKHD2 - Hongkong </td> <td>PidMonRsp</td> </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>OCOMsg2</td> </tr>

this is the data Dumper

print Dumper @stat_array $VAR1 = [ 'ibfarm102 localtick', ' Greenwich', ' hibmis100 procHKHD2 Hongkong ', 'PidMonRsp' ]; print Dumper @stat_array_rows. $VAR1 = 'ibfarm102 localtick'; $VAR2 = ' Greenwich'; $VAR3 = ' hibmis100 procHKHD2 Hongkong '; $VAR4 = 'PidMonRsp';

Comment on perl generated table - format color
Select or Download Code
Re: perl generated table - format color
by Anonymous Monk on Jul 26, 2011 at 08:37 UTC
    And what are the results that you want (html)?
Re: perl generated table - format color
by choroba (Canon) on Jul 26, 2011 at 09:34 UTC
    Just move the condition outside the loop; if you know what member of a row to test, test just that (i.e. $stat_array_rows[2], otherwise you can use grep. Your code is buggy: why do you use $stat_row[0], when the only stat_row variable is a scalar? Use strict and warnings!
Re: perl generated table - format color
by Anonymous Monk on Jul 26, 2011 at 13:52 UTC
    The best way to get "red," etc. is with a CSS stylesheet. Let the Perl script output the data without regard to how it looks, but adding "class=" if necessary.
Re: perl generated table - format color
by scorpio17 (Monsignor) on Jul 26, 2011 at 18:22 UTC

    As you loops through your data, you need to determine if a given row contains the "OCOMsg2" field you're looking for, and if it does write out the following:

    <tr style="color: red;">

    Otherwise, just write out a plain tr tag with no style attribute. You need to do this before splitting the row into individual parts - or else use join to put them back together in order to do the check. Something like this:

    use strict; 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" ); while (my @group_of_4 = splice(@stat_array, 0 , 4)) { my $rowstring = join(' ', @group_of_4); if ($rowstring =~ /OCOMsg2/) { print MAIL "<tr style=\"color: red;\">\n"; } else { print MAIL "<tr>\n"; } for my $data (@group_of_4) { print MAIL "<td>$data</td>\n"; } print MAIL "</tr>\n"; }

    I get this output:

    <tr> <td>ibfarm102 - localtick</td> <td>Boston</td> <td>hibmis100 - procHKHD2 - Hongkong</td> <td>PidMonRsp</td> </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 style="color: red;"> <td>ibfarm102 - localtick</td> <td>New York</td> <td>hibmis100 - procHKHD2 - Hongkong</td> <td>OCOMsg2</td> </tr>

    If that's not what you want - hopefully it's enough to get you going in the right direction.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://916701]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2015-07-04 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls