bfdi533 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to build a hash depending on the in a database table that contains a 1 or 0 depending if there is data for a given element.
Given the following input table:
id fname lname 1 Joe Smith 2 Sally Smith
Here is the code I am using:
use DBI; $dbh = DBI->connect("DBI:CSV:f_dir=./"); $query = "select id, fname, lname"; $sth->prepare($query); $sth->execute(); while ($rowdata = $sth->fetchrow_hashref) { $hash{$rowdata->{'id'}} = $rowdata{'id'}; if ($rowdata->{'fname'}) { $hash{$rowdata->{'id'}}{'fname'} = 1; } if ($rowdata->{'lname'}) { $hash{$rowdata->{'id'}}{'lname'} = 1; } } print Dumper(\%hash);
Yet, I do not get the expected results. I get the following:
$VAR1 = { '' => undef };
I would expect to see something like the following (constructed my myself and may or may not be valid):
$VAR1 = { { '' => 1 'fname' => 'John' 'lname' => 'Smith' { { '' => 2 'fname' => 'Sally' 'lname' => 'Smith' { };
Update: Changed all references to $rowdata{} to $rowdata->{}.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: building a hash from a database
by bart (Canon) on May 02, 2006 at 15:34 UTC | |
by bfdi533 (Friar) on May 02, 2006 at 15:42 UTC | |
by bart (Canon) on May 02, 2006 at 15:44 UTC | |
by bfdi533 (Friar) on May 02, 2006 at 16:20 UTC | |
|
Re: building a hash from a database
by zer (Deacon) on May 02, 2006 at 15:32 UTC | |
by bfdi533 (Friar) on May 02, 2006 at 15:35 UTC | |
|
Re: building a hash from a database
by Codon (Friar) on May 02, 2006 at 16:10 UTC | |
|
Re: building a hash from a database
by runrig (Abbot) on May 02, 2006 at 15:44 UTC | |
|
Re: building a hash from a database
by duff (Parson) on May 02, 2006 at 15:35 UTC |