> First let's let the other list member in on the code:
> 
> use DBI;
> use strict;
> my $dbname = "test";
> my $host = "localhost";
> my $dbuser = '';
> my $dbpw = '';
> 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 * from method";

#replace prev. line with:
my $sql = 'select methodid,method,sname from method';
   $sql .= '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;
> my %thehash;
> my @row_ary;
> my $row_ary;
> my $key;
> 
> while (@row_ary  = $sth->fetchrow_array) {
>  $key = $row_ary[0];
>  $thehash{$key} = $row_ary[1];

#replace prev. two lines with:
$ra{$row_ary[0]} = [$row[1], $row[2]];

> }
> $sth->finish;
> $dbh->disconnect;

This builds a hash of n two-element arrays, where n is "select
count(methodid) from method". You can access the hash using (as the
first index) the methodid you want, and the second index will be either
0 or 1, depending whether you want the method or sname (respectively)
for that methodid:

#based on the table printout from the first post:
print $ra->{2}->[0];
# prints 'Progestin-Only Ocs'
Print $ra->{2}->[1];
# prints 'POC'

The reason I picked a hash of arrays is so it doesn't matter whether
there are always as many rows as max(methodid) (i.e. no gaps in the
sequence, otherwise you could use an array - see below), and the
two-element array because you don't need the associative array
functionality. You could just as easily create two local variables
($method,$sname) = (0,1) and access the array using the variables as the
index.

If you wanted an array of arrays:

$ra[$row_ary[0]] = [$row[1], $row[2]];

#based on the table printout from the first post:
print $ra->[2]->[0];
# prints 'Progestin-Only Ocs'
Print $ra->[2]->[1];
# prints 'POC'

Would do the trick. Either way should work, it just depends on how you
want to access your data :) check out
<http://www.perldoc.com/perl5.6.1/pod/perlreftut.html>.

Hope that helps,

 -dave



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to