Thank you, David Gray and David K for all your help with understanding
hashes. David Gray, the article you referenced at
<http://www.perldoc.com/perl5.6.1/pod/perlreftut.html> was very
helpful. Thank you also for the hash of arrays solution which was just
exactly what I was thinking of. Thank you David K for showing me some
excellent SQL techniques, in addition to the program itself.
For those following the thread, and others searching later, here's a
final working program which illustrates the technique. Below it is a
dump of the database used in this example.
�----------------------------------------------------------------------------------------------------------
www:/cire # cat methodtest4.pl
#!/usr/local/bin/perl -w
&get_method;
our %thehash;
# all dbi stuff is done, show the hash
foreach $key (sort keys %thehash) {
print"Key: $key Method name : $thehash{$key}[0] Method
short name: $thehash{$key}[1]\n";
}
sub get_method {
use DBI;
use DBD::mysql;
use strict;
my $dbname = "cire"; # enter your db name
my $host = "localhost";
my $dbuser = 'cire'; # user may be required
my $dbpw = 'xxxxxx'; # pw may be required
my $mscs = "dbi:mysql:dbname=$dbname;host=$host;";
my $dbh = DBI->connect($mscs, $dbuser, $dbpw) or die "Connect fails
to $dbname\nError = ", DBI::errstr;
my $sql = "select methodid, method, sname from method ORDER BY
methodid";
my $sth = $dbh->prepare($sql) or die "Prepare fails for
stmt:\n\t\t$sql\nError = ", DBI::errstr;
my $rv;
unless ($sth->execute) {
print"\n\tExecute fails for stmt:\n\t\t$sql\nError = ",
DBI::errstr;
$sth->finish;
$dbh->disconnect;
die "\n\t\tClean up finished\n";
}
print "\t\t$rv\n\n" if $rv;
our %thehash;
my @row_ary;
while (@row_ary = $sth->fetchrow_array) {
#$key = $row_ary[0];
#$thehash{$key} = $row_ary[1];
$thehash{$row_ary[0]} = [$row_ary[1], $row_ary[2]];
print "Key: $row_ary[0] $thehash{$row_ary[0]}[0]
$thehash{$row_ary[0]}[0]\n"; #for debugging use only
}
$sth->finish;
$dbh->disconnect;
} # sub get_method
�---------------------------------------------------------------------------------------------------------
mysql> select * from method;
+----------+------------------------------------+---------+
| methodid | method | sname |
+----------+------------------------------------+---------+
| 1 | Combined OCs | COC |
| 2 | Progestin-Only OCs | POC |
| 3 | DMPA/NET EN | DMPA |
| 4 | Norplant Implants | NI |
| 5 | Female Sterilization | FS |
| 6 | Vasectomy | Vas |
| 7 | Condoms | Condoms |
| 8 | TCu-380A IUD | TCu |
| 9 | Spermicides | Sperm |
| 10 | Diaphragm Cervical Cap | DCC |
| 11 | Fertility Awareness-based Methods | FABM |
| 12 | Lacational Amenorrhea Method (LAM) | LAM |
+----------+------------------------------------+---------+
12 rows in set (0.00 sec)
mysql>
�---------------------------------------------------------------------------
Output of program:
www:/cire # ./methodtest4.pl
Key: 1 Combined OCs Combined OCs
Key: 2 Progestin-Only OCs Progestin-Only OCs
Key: 3 DMPA/NET EN DMPA/NET EN
Key: 4 Norplant Implants Norplant Implants
Key: 5 Female Sterilization Female Sterilization
Key: 6 Vasectomy Vasectomy
Key: 7 Condoms Condoms
Key: 8 TCu-380A IUD TCu-380A IUD
Key: 9 Spermicides Spermicides
Key: 10 Diaphragm Cervical Cap Diaphragm Cervical Cap
Key: 11 Fertility Awareness-based Methods Fertility
Awareness-based Methods
Key: 12 Lacational Amenorrhea Method (LAM) Lacational Amenorrhea
Method (LAM)
Key: 1 Method name : Combined OCs Method short name: COC
Key: 10 Method name : Diaphragm Cervical Cap Method short name: DCC
Key: 11 Method name : Fertility Awareness-based Methods Method
short name: FABM
Key: 12 Method name : Lacational Amenorrhea Method (LAM) Method
short name: LAM
Key: 2 Method name : Progestin-Only OCs Method short name: POC
Key: 3 Method name : DMPA/NET EN Method short name: DMPA
Key: 4 Method name : Norplant Implants Method short name: NI
Key: 5 Method name : Female Sterilization Method short name: FS
Key: 6 Method name : Vasectomy Method short name: Vas
Key: 7 Method name : Condoms Method short name: Condoms
Key: 8 Method name : TCu-380A IUD Method short name: TCu
Key: 9 Method name : Spermicides Method short name: Sperm
www:/cire #
Thanks, again.
-Kevin
-----
E. Kevin Zembower
Unix Administrator
Johns Hopkins University/Center for Communications Programs
111 Market Place, Suite 310
Baltimore, MD 21202
410-659-6139
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]