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"; }

In reply to Re: Why is my code assigning the last-retrieved value to all elements in my hash? by kyle
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.