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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
alternate table colors

 



parham_m_s
journeyman

Apr 22, 2001, 6:31 PM

Post #1 of 4 (502 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.

Code
*|  
|*
*|
|*
*|

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.

Code
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>
</TR>
EOF

} else {

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

}

$count++;

}

print "</TABLE>";



randor
User

Apr 23, 2001, 6:01 AM

Post #2 of 4 (490 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>~;
$aa++}
}

Randor





rjoseph
Novice

May 5, 2001, 6:17 PM

Post #3 of 4 (472 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:

Code
$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:

Code
my $linecount=0; 

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

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


Kanji
User

May 7, 2001, 8:23 PM

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

Another way to do this is ...


Code
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