I'm having a hard time understanding and getting started with hashes. I
don't think I get the concept, and so can't go from the concept to the
actual code.
I have a MySQL database table called "methods" with three fields,
methodid, method, and sname. I'd like to read them into a hash, so that
I could refer to them there (in memory) rather than repeatedly reading
the database.
Here's the output of the table:
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.01 sec)
(I work in the field of reproductive health. Can you tell?)
I think I can read in the table with this code, but don't know how to
build it into a hash:
use DBI;
our $dbh = DBI->connect("dbi:mysql:cire:localhost", "cire", "password",
{RaiseError => 1, AutoCommit => 0}) or die;
my $method_arrayref = $dbh->selectall_arrayref("SELECT methodid,
method, sname FROM method ORDER BY methodid");
[Don't know what to do here to build it into a hash, that I can
reference by explicitly stating the methodid, then using the method or
sname.]
Instead of the selectall_arrayref, I can use a while loop and read the
table one row at a time, using fetchrow_arrayref, but I still don't know
how to use that to build the hash. Would this method be easier?
Finally, when I've got the hash built, how can I get a particular
method, by specifying it's methodid, or instance?
Thanks for your help and suggestions. Please let me know if I should
take this question to the DBI-users list.
-Kevin Zembower
-----
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]