Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^8: 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 22:32 UTC ( [id://696580]=note: print w/replies, xml ) Need Help??


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

Thanks a heap for taking time to deal with this completely and in detail.

I do believe that the truly salient point is this:

There's one other misconception you seem to have that I'd like to clear up. 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}. In your original code, that "reference" was "1", treated as a symbolic reference to %1. In the code I suggested, it's a hard reference to an anonymous hash that gets printed as 'HASH(0x8529f88)'.

This appears to be the real root of the problem. I started with a one-dimensional hash, then extended it to two dimensions. $booklist_1{$book_id} therefore becomes a reference to a hash and I can no longer use it the way I was trying to.

On this basis, I changed it to

while ($book_id = $sth->fetchrow_array()) { $booklist_1{$book_id}{'book_id'} = 1; }
and that works perfectly.



Time flies like an arrow. Fruit flies like a banana.
  • Comment on Re^8: Why is my code assigning the last-retrieved value to all elements in my hash?
  • Download Code

Replies are listed 'Best First'.
Re^9: Why is my code assigning the last-retrieved value to all elements in my hash?
by kyle (Abbot) on Jul 10, 2008 at 03:30 UTC

    Unless there's some reason for wanting '1', I'd put the actual book ID in the 'book_id' slot of that hash.

    while (($book_id) = $sth->fetchrow_array()) { $booklist_1{$book_id}{'book_id'} = $book_id; }

    Unless you have a book ID of '0', it will still be true in boolean tests, and it will be less confusing to future programmers.

      Getting too squished on my screen - see bottom of thread for my conclusion in the light of the next day....




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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://696580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found