my $i=0; while ($pointer = $sth->fetchrow_hashref){ my $class = ("even","odd")[$i++%2]; ...more stuff goes here.... }