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->{}.
In reply to building a hash from a database by bfdi533
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |