Ask a Question related to PERL Miscellaneous, Design and Development.
-
Donavon #1
Database Record into a hash...
I am trying to load a database into a HASH of HASH. For some reason
every instance of the HASH only contains the data from the last record
pulled?
So I have a 1000 copies of the same information with different HASH
keys.
Any suggestions??
....
....
....
# loads a record into a hash
while ($data = $sth->fetchrow_hashref) {
%record = %$data;
$key = $record{'ProjectNumber'};
$hash{$key} = \%record;
};
$sth -> finish;
$dbh -> disconnect
or warn "Disconnection failed: $DBI::errstr\n";
# prints out content of HASH
foreach $key1 (keys %hash) {
print 'Level 1: ' . $key1 . "<BR>";
foreach $key2 (keys %{$hash{$key1}}) {
print $key2 . '=>' . $hash{$key1}{$key2} . "<BR>";
};
};
Donavon Guest
-
Check the record in Database using ASP.NET
Dear All, Dreamweaver user who want to develop a site using ASP.NET have got critical time. In Server behavious we doesn't have User... -
database row in a hash
Hi, I'm working woth the ruby postgresql interface. Due to my previous use of other languages, I am used to have access to a retrieved row in the... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
editing record in access database
I am trying to pull a record into a form and then edit it. (not working to well) Hoping someone can show me a better way than this: <% option... -
Database record retrieving
> They have links to actually, they do, but they just use a more complex layout, among other things. for instance, the center column would be... -
Tad McClellan #2
Re: Database Record into a hash...
Donavon <djlerman@yahoo.com> wrote:
> I am trying to load a database into a HASH of HASH. For some reason
> every instance of the HASH only contains the data from the last record
> pulled?> Any suggestions??
use strict;
> %record = %$data;
my %record = %$data;
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Chief Squawtendrawpet #3
Re: Database Record into a hash...
Donavon wrote:
Each time through the loop, the key-value pairs in %record may be> while ($data = $sth->fetchrow_hashref) {
> %record = %$data;
> $key = $record{'ProjectNumber'};
> $hash{$key} = \%record;
> };
changing, but %record itself is still the same old hash to which all
of the refs in %hash keep pointing.
One way to solve your problem is to dispense with the intermediary
(%record) and work directly with the ref in $data.
$hash{ $data->{ProjectNumber} } = $data;
Another way to solve the problem is to take Tad's advice:
my %record = %$data;
This has the effect of defining a "fresh copy" of %hash each time
through the loop -- at least that's how I lamely understand it. Maybe
one of the experts around here could offer a better explanation,
becuase this behavior has always been a little bit mysterious to me.
Chief S.
Chief Squawtendrawpet Guest
-
Thomas Kratz #4
Re: Database Record into a hash...
"Donavon" <djlerman@yahoo.com> wrote in
news:97ba267a.0309171038.79aeef1e@posting.google.c om...reason> I am trying to load a database into a HASH of HASH. For somerecord> every instance of the HASH only contains the data from the lastHASH> pulled?
>
> So I have a 1000 copies of the same information with differentwhile (my $data = $sth->fetchrow_hashref) {> keys.
> Any suggestions??
>
> ...
> ...
> ...
> # loads a record into a hash
> while ($data = $sth->fetchrow_hashref) {
# create a new hashref for each loop
delete the above line, you do not need a temporary hash.>
> %record = %$data;
$hash{$data->{'ProjectNumber'}} = $data;> $key = $record{'ProjectNumber'};
> $hash{$key} = \%record;
A more elegant alternative is:>
> };
>
> $sth -> finish;
my $hashref;
unless ( $hashref = $sth->fetchall_hashref('ProjectNumber') ) {
warn "couldn't fetch data, $DBI::errstr\n";
}
# let DBI do the loop and create the hash
# no need to call $sth->finish() now
foreach $key1 (keys %$hashref) {> $dbh -> disconnect
> or warn "Disconnection failed: $DBI::errstr\n";
>
> # prints out content of HASH
> foreach $key1 (keys %hash) {
foreach $key2 (keys %{$hashref->{$key1}}) {> print 'Level 1: ' . $key1 . "<BR>";
>
> foreach $key2 (keys %{$hash{$key1}}) {
> print $key2 . '=>' . $hash{$key1}{$key2} . "<BR>";
print $key2 . '=>' . $hashref->{$key1}{$key2} . "<BR>";
(untested, more in 'perldoc DBI')> };
> };
Thomas
Thomas Kratz Guest



Reply With Quote

