in reply to Why is my code assigning the last-retrieved value to all elements in my hash?
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
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.while ($book_id = $sth->fetchrow_array()) { $booklist_1{$book_id} = 1; }
Where it gets messed up is in the second loop where I extend the hash
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$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;
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:
Note the "_temp" added to the booklist_1 hash name in the first loop. Which works like a charm.$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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why is my code assigning the last-retrieved value to all elements in my hash?
by Porculus (Hermit) on Jul 10, 2008 at 20:15 UTC | |
|
Re^2: Why is my code assigning the last-retrieved value to all elements in my hash?
by kyle (Abbot) on Jul 10, 2008 at 20:56 UTC | |
|
Re^2: Why is my code assigning the last-retrieved value to all elements in my hash?
by polettix (Vicar) on Jul 11, 2008 at 08:45 UTC |