bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
For the entire script:while (@data = $sth->fetchrow_array()) { if ($data[1] ne $tempstate) { undef (@cities); push (@cities, $data[0]); $states{$data[1]} = [ @cities ]; $tempstate = $data[1]; $tempcity = $data[0]; } elsif ($data[0] ne $tempcity) { push (@cities, $data[0]); $states{$tempstate} = [ @cities ]; $tempcity = $data[0]; } }
#!/usr/bin/perl print "Content-type: text/html\n\n"; use warnings; use CGI::Carp qw(fatalsToBrowser); use strict; my ($dbh, $sth, $stmt, $tempstate, $tempcity, %states, @data, @cities, + $city, $x); &dbconnect; #connect to database subroutine $stmt = "SELECT city, state, country FROM sponsor ORDER BY country, + state, city"; $sth = $dbh->prepare($stmt) or die "prepare: $stmt: $DBI::errstr"; $sth->execute() or die"execute: $stmt: $DBI::errstr"; while (@data = $sth->fetchrow_array()) { if ($data[1] ne $tempstate) { @cities = ""; push (@cities, $data[0]); $states{$data[1]} = [ @cities ]; $tempstate = $data[1]; $tempcity = $data[0]; } elsif ($data[0] ne $tempcity) { push (@cities, $data[0]); $states{$tempstate} = [ @cities ]; $tempcity = $data[0]; } } for my $state ( keys %states ) { print "$state:<br />"; for my $i (1..$#{ $states{$state} } ) { print "$i = $states{$state}[$i]<br />"; } } $sth->finish(); $dbh->disconnect();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Better code for normalizing a list for a HoA?
by merlyn (Sage) on Jan 17, 2004 at 21:05 UTC | |
by bradcathey (Prior) on Jan 17, 2004 at 21:30 UTC | |
by merlyn (Sage) on Jan 17, 2004 at 21:33 UTC | |
by bradcathey (Prior) on Jan 17, 2004 at 23:50 UTC |