Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by kyle (Abbot)
on Jul 09, 2008 at 18:53 UTC ( [id://696522]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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?

Use strict and warnings!!!

use Data::Dumper; my %booklist_l; $booklist_l{ 'foo' } = 1; $booklist_l{ 'bar' } = 1; $booklist_l{ 'foo' }{ 'update_id' } = 'update_id 1'; $booklist_l{ 'foo' }{ 'title' } = 'title 1'; $booklist_l{ 'bar' }{ 'update_id' } = 'update_id 2'; $booklist_l{ 'bar' }{ 'title' } = 'title 2'; print Dumper \%booklist_l; print Dumper \%1; __END__ $VAR1 = { 'bar' => 1, 'foo' => 1 }; $VAR1 = { 'update_id' => 'update_id 2', 'title' => 'title 2' };

That number '1' you put into every entry is being treated as a symbolic reference to the variable %1.

Replies are listed 'Best First'.
Re^4: 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 19:08 UTC
    Excellent.

    So, since the '1' just serves to give a value that allows the element to exist, setting to something, anything, else should serve teh same purpose and avoid the undesired symbolic reference to %1.

    Went ahead and changed it to

    while ($book_id = $sth->fetchrow_array()) { $booklist_1{$book_id} = "ok"; }
    but it made no difference. Same output.

    I really appreciate the attention to my prob, though. Did I miss-apply what you were trying to say?




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

      Did I miss-apply what you were trying to say?

      Yes.

      Change "ok" to {}, and that should work.

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

      Even then, I urge you to Use strict and warnings. It would have saved you from creating this problem in the first place. Instead of silently doing what you didn't want, it would have told you on its death bed,

      Can't use string ("1") as a HASH ref while "strict refs" in use

      It's not the best error message in this case, but it would have served better than the mystery you had. If you'd not been able to figure it out, you could have posted it here, and we would have been able to answer immediately rather than groping around.

      So I say a third time, Use strict and warnings

        Oh I agree about strict and warnings - but, frankly, I'm not sure that warning would have helped alot. 'Cause if the problem is that the " = 1" in
        while ($book_id = $sth->fetchrow_array()) { $booklist_1{$book_id} = 1; }
        creates a reference to %1, then why doesn't using something other than "1" correct the problem?

        I mean, I just want to set the value of a hash element to one.

        I love Perl - it's responsible for most of my personal income and written into most of my work by choice - so don't get me wrong on this next statement - but on a rare occasion Perl does really make me shake my head. It's not like I'm trying to invent calculus here. I just want to set the value of a hash element to 1 then recall the key. It shouldn't take strict and warnings and an in-depth discussion of special variables and references and most of the afternoon...this sort of thing is just ammunition for the anti-Perl crowd...ok, venting off...

        By the way, using {} did make the script work correctly, with the sole exception that now $booklist_1{$book_id} equals 'HASH(0x8529f88)' - which is not a problem, per se, since I don't use it, but it seems a very messy result.




        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://696522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found