Database Record into a hash...

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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 = {...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default Re: Database Record into a hash...

    Donavon wrote:
    > while ($data = $sth->fetchrow_hashref) {
    > %record = %$data;
    > $key = $record{'ProjectNumber'};
    > $hash{$key} = \%record;
    > };
    Each time through the loop, the key-value pairs in %record may be
    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

  5. #4

    Default Re: Database Record into a hash...


    "Donavon" <djlerman@yahoo.com> wrote in
    news:97ba267a.0309171038.79aeef1e@posting.google.c om...
    > 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) {
    while (my $data = $sth->fetchrow_hashref) {
    # create a new hashref for each loop
    >
    > %record = %$data;
    delete the above line, you do not need a temporary hash.
    > $key = $record{'ProjectNumber'};
    > $hash{$key} = \%record;
    $hash{$data->{'ProjectNumber'}} = $data;
    >
    > };
    >
    > $sth -> finish;
    A more elegant alternative is:

    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
    > $dbh -> disconnect
    > or warn "Disconnection failed: $DBI::errstr\n";
    >
    > # prints out content of HASH
    > foreach $key1 (keys %hash) {
    foreach $key1 (keys %$hashref) {
    > print 'Level 1: ' . $key1 . "<BR>";
    >
    > foreach $key2 (keys %{$hash{$key1}}) {
    > print $key2 . '=>' . $hash{$key1}{$key2} . "<BR>";
    foreach $key2 (keys %{$hashref->{$key1}}) {
    print $key2 . '=>' . $hashref->{$key1}{$key2} . "<BR>";
    > };
    > };
    (untested, more in 'perldoc DBI')

    Thomas


    Thomas Kratz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139