in reply to Why is my code assigning the last-retrieved value to all elements in my hash?
My guess is there's a reference somewhere you're not expecting. For example, if %booklist_l is initialized this way:
my %booklist_l; @booklist_l{ @book_id_list } = ( { title => 'empty', update_id => 'missing' } ) x scalar @book_id_list;
...then every item in %booklist_l has the same reference to an "empty" record.
Also, DBI, when it returns things from fetchrow_hashref, always returns the same reference (as a performance optimization). As such, if you do this:
$booklist_l{ $book_id } = $sth->fetchrow_hashref;
...again, you'll have the same reference everywhere. In that case, you can get a copy of what it returns like this:
$booklist_l{ $book_id } = { %{$sth->fetchrow_hashref} };
That doesn't look like what you're doing, but I can't run your example code, so I'm suspicious.
Update: I think I didn't explain my suspicion very well. Each $booklist_l{ $book_id } is a reference to a hash. I suspect they're all the same reference. How that happened, I'm not sure, but I give a couple of possibilities above. An easy way to check whether they're all the same reference is to stringify them. Your ending debugging loop could be:
foreach $book_id (keys %booklist_1) { my $r = $booklist_l{$book_id}; print "XXX $book_id > $r->{'update_id'} -- from $r\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why is my code assigning the last-retrieved value to all elements in my hash?
by punch_card_don (Curate) on Jul 09, 2008 at 18:37 UTC | |
by kyle (Abbot) on Jul 09, 2008 at 18:53 UTC | |
by punch_card_don (Curate) on Jul 09, 2008 at 19:08 UTC | |
by kyle (Abbot) on Jul 09, 2008 at 19:20 UTC | |
by punch_card_don (Curate) on Jul 09, 2008 at 20:44 UTC | |
| |
by karavelov (Monk) on Jul 09, 2008 at 19:40 UTC |