PerlAccess.com : Input and Output
Alternating Table Row Colors
by Kevin Pruggles
http://www.e-pixs.com/funchat

This short tutorial will show you how to create tablized data with alternately colored rows. Using alternate colors makes reading tablized data easier and visually more attractive. Using this method frees you from having to hard code the bgcolor of each row and not to have to worry about how many lines are in the flat file. The code will count the lines and match the last number of each line and assign a bgcolor depending if the match was an even number and default to another color if the value is not an even number. So all we really need do is see if the last number for each line in the file is a 0, 2, 4, 6 or 8.
We will assume you are using a CSV flat file with three values per line: Name, Age, Sex. 


open (FILE, "../userdata.txt");
@DATA=<FILE>;
close(FILE);
print (qq(Content-type: text/html
<html><head><title>Your Title</title></head>
<body bgcolor=#E6E6FA>
<table width=95% border=2 cellpadding=1 cellspacing=0
bordercolor=#000099 align=center> <tr bgcolor=#000099><td> <font face=verdana color=#DDDDDD size=2><b> Name </b></font>
</td> <td><font face=verdana color=#DDDDDD size=2><b> Age </b></font>
</td> <td><font face=verdana color=#DDDDDD size=2><b> Sex </b></font>
</td></tr>)); $total = 0; # sets the count to zero foreach $line (@DATA) {    ($name,$age,$sex) = split(/,/, $line);    $total++; # increments the count for each line in the file    if ($total =~ /[0,2,4,6,8]$/) {# matches the end of the count value       $bgcolor = "#EEEEEE"; # assigns the color if the pattern matches    }    else {       $bgcolor = "#CCCCCC";
# assigns the default color if the pattern did not match
   }    print (qq(<tr bgcolor=$bgcolor><td><font face=verdana size=1> $name
</td> <td><font face=verdana size=1> $age </td> <td><font face=verdana size=1> $sex </td></tr>)); } print qq(</table></body></html>);