Bob Showalter am Samstag, 25. Februar 2006 15.03: > henry chen wrote: > > I can't seem to figure out how to alternate the bgcolor for each row > > that i'm printing from arrayref. Is there a 'foreveryother' function > > that would allow me to put two lines in the loop? Or is there someway I > > can put two rows of information and loop that? Here's what I have so > > Instead of tracking an odd/even row number, you can initialize two > colors before the loop: > > my ($ca, $cb) = ('red', 'green'); # Christmas-y! > > Then inside the loop, always use $ca for the color, and just swap the > colors after you use them: > > while (...blah) { > > print qq[<tr style="background: $ca">]; > ($ca, $cb) = ($cb, $ca); > > } > > HTH
Hi together The different answers are very interesting, I'd never thought of the last two! I'd like to add another, which is also usable with more than two colors. I would very much appreciate improvements, since it's the first time I use overload :-) ===== package ColorAlternator; # Cycles through a number of colors use strict; use warnings; use overload '""' => \&color; sub new { my $class=shift; bless {c=>[EMAIL PROTECTED], i=>0, n=>[EMAIL PROTECTED], $class; } sub color { my $self=shift; my $c=$self->{c}->[$self->{i}]; $self->{i}=++$self->{i} % $self->{n}; $c; } package main; my $color=ColorAlternator->new( qw[ #303030 #000000 #eeeeee] ); for my $row ( 1..10 ) { print "color is $color\n"; } ===== Hans -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>