$sth->execute() or die "Cannot execute: " . $sth->errstr(); my %hash; while ( my( $district, $ID ) = $sth->fetchrow_array ) { $hash{ $district } = $ID; } # A number of ways to do it here, this is the best if you # don't know you have an exact multiple of 3.... my @keys = sort keys %hash; # sort by district my $column=0; print "\n"; while ( my $dis = shift @keys ) { if ( $column == 0 ) { print "\n"; } print ""; if ( $column == 2 ) { print "\n"; $column = 0; } else { $column++; } } # Print trailing TR if needed if ( $column != 0 ) { print "\n"; } print "
", $dis, "
\n";