Soooooo....Many thanks to Kyle and others for sticking with this even when I got crabby yesterday.

Really the central problem was extending a one-dimensional hash to a two-dimensional one and then expecting the first-dimension's keys to remain scalars. Having extended the original hash into a hash of hashes, clearly the first-dimension's keys had to become hash-refs.

With all due respect to Kyle, I don't believe it has anything at all to do with using "1".

The booklist_1 hash populating loop

while ($book_id = $sth->fetchrow_array()) { $booklist_1{$book_id} = 1; }
works just fine. There'd be something seriously fubar if you couldn't set the value of a variable to 1. At this point, keys %booklist_1 gives me the list of book_id's.

Where it gets messed up is in the second loop where I extend the hash

$sql = "select update_id, title from booklist_table where (book_id = ? +) order by update_id desc limit 1"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; foreach $book_id (keys %booklist_1) { $sth->execute($book_id) or die("Could not execute!" . $dbh->errstr +); ($update_id, $title) = $sth->fetchrow_array(); $booklist_1{$book_id}{'update_id'} = $update_id; $booklist_1{$book_id}{'title'} = $title; print "$book_id > $booklist_1{$book_id}{'update_id'}\n"; } $sth->finish;
When I extend it to two dimensions, it becomes a hash of hashes and the first dimension's keys must become references to the second dimenion hashes. So now keys %booklist_1 is a bunch of hash references, and no longer the bunch of book_ids I set them to originally. Kyle set me onto this when he wrote

You can't have a $b{foo}{bar} and have $b{foo} set to some unrelated value (such as 1). If there is a $b{foo}{bar}, then $b{foo} must somehow be a reference to a hash where you access ->{bar}.

My final solution is to use distinct hashes, like this:

$sql = "SELECT book_id FROM booklist_table WHERE (expected_publcn_date + >= '".$start_date."' AND expected_publcn_date <= '".$end_date."')"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; $sth->execute() or die("Could not execute!" . $dbh->errstr); while ($book_id = $sth->fetchrow_array()) { $booklist_1_temp{$book_id} = 1; } $sth->finish; $sql = "select update_id, title from booklist_table where (book_id = ? +) order by update_id desc limit 1"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; foreach $book_id (keys %booklist_1_temp) { $sth->execute($book_id) or die("Could not execute!" . $dbh->errstr +); ($update_id, $title) = $sth->fetchrow_array(); $booklist_1{$book_id}{'update_id'} = $update_id; $booklist_1{$book_id}{'title'} = $title; print "$book_id > $booklist_1{$book_id}{'update_id'}\n"; } $sth->finish;
Note the "_temp" added to the booklist_1 hash name in the first loop. Which works like a charm.




Time flies like an arrow. Fruit flies like a banana.

In reply to Re: Why is my code assigning the last-retrieved value to all elements in my hash? by punch_card_don
in thread Why is my code assigning the last-retrieved value to all elements in my hash? by punch_card_don

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.