my $ctr = 0; my $class; while( $pointer = $sth->fetchrow_hashref ) { $class = ( ($ctr++) % 2 == 0 ) ? "even" : "odd"; ## ... }