Welcome to the Monastery | |
PerlMonks |
Re: Why is my code assigning the last-retrieved value to all elements in my hash?by punch_card_don (Curate) |
on Jul 10, 2008 at 12:51 UTC ( [id://696686]=note: print w/replies, xml ) | Need Help?? |
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 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 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: 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 Section
Seekers of Perl Wisdom
|
|