CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
alternate table colors



Apr 22, 2001, 6:31 PM

Post #1 of 4 (1095 views)
alternate table colors Can't Post

I have a table with two columns. I've scrambled myself to highlight alternate table rows on each column.


i have the following code, and i was wondering if anyone could help me achieve the effect i'm looking for. I included the count on each cell because i thought that would help me come up with a solution, which it didn't.

print "<TABLE>\n\n"; 

@a = ('one','two','three','four','five','six','seven','eight','nine','ten','eleven','twleve','thirteen','fourteen');

$count = 1;

foreach $a (@a) {

if ($count / 2 == int($count / 2)) {
print <<"EOF";
<TD>$count - $a</TD>

} else {

print <<"EOF";
<TD> $count - $a</TD>




print "</TABLE>";


Apr 23, 2001, 6:01 AM

Post #2 of 4 (1083 views)
Re: alternate table colors [In reply to] Can't Post

ok, i am pretty sure this is what you are looking for.. mind you it is a bit sloppy, but it will get the affect you want, and it is quick:

@a = ('one','two','three','four','five','six','seven','eight','nine','ten','eleven','twleve','thirteen','fourteen');
$aa = '1';
foreach $a (@a) {
if ($aa == '1') { print qq~<TD>$count - $a</TD></TR>~;
$aa--;}else {print qq~<TR><TD> $count - $a</TD>~;



May 5, 2001, 6:17 PM

Post #3 of 4 (1065 views)
Re: alternate table colors [In reply to] Can't Post

I don't think this will work - you're not accomplishing what he asked.

The best way to do this is to use the modulo (%) operator. What is does is return the remainder from a division operation, so:

$a = 3%2; 
print $a;

Would print 1 (3 divided by two yields a remainder of one). How does this help you, you ask? Well, it helps because anytime you divide a number by a certain multiple, you get zero.

So you need to alternate every other lines - that means that there will be two possibilities (color one or color two). What you can do is then have a count of the lines you are moving through, and modulo this count by 2 - everyother line, it will equal zero. Therefore:

my $linecount=0; 

foreach (@a) {
if ($linecount%2) { print "something"; }
else { print "something different"; }

In the if {} above, the remainder will equal one (true) while in the else it will be zero (false) - there is your alternation!

Hope this helps!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


May 7, 2001, 8:23 PM

Post #4 of 4 (1053 views)
Re: alternate table colors [In reply to] Can't Post

Another way to do this is ...

my @colours = ( '#cccccc', '#eeeeee' ); 
my $colour = 0;
foreach my $row (@rows) {
print qq(<tr bgcolor="$colours[$colour]"><td>$row</td></tr>);
$colour = ! $colour;

This works because $colour = ! $colour acts as a boolean switch, so when $colour is 0 (aka 'false'), ! $colour is 1 (aka 'true'), and vice-versa.

This method only works with two alternating colours, 'though. If you want to alternate 3 or more colours, then modulo is definitely the way to go.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives