http://qs321.pair.com?node_id=838553

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have this part of my code here to demonstrated what I am trying to do, which is alternating the colors of the rows based on the query results. I know there are many ways of doing this using odd and even numbers, but because of the nature of this particular query results the values of the counter "$i" will not be in order or sequence, this block of code will print the values of $i as:
0 2 14 15 16 19 20
I am thinking that the only way is to compare the values of $i and if they are different assign a value I could use to do the color alternation, but I am running out of ideas, any help would be very appreciated, thanks for the help!
for (my $i = 0; $i < @$sql; $i++) { my $user = $sql->[$i]{user}; if($unique{$user}) { next; } $unique{$user} = 1; my $show = "<a href=\"show.pl?name=$name\">$name</a>"; my $first = $sql->[$i]{first}; my $last = $sql->[$i]{last}; my $tel = $sql->[$i]{tel}; my $email = $sql->[$i]{email}; + if ($i % 2) { $data = "$data<tr><td>$last, $first</td> <td>$show</td> <td>$email</td> </tr>"; }else { $data = "$data<tr><td>$last, $first</td> <td>$show_link</td> <td>$email</td> </tr>"; } }