Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

delete hash element while traversing via values coredumped

by jesuashok (Curate)
on Jun 08, 2006 at 08:27 UTC ( [id://554235]=perlquestion: print w/replies, xml ) Need Help??

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: delete hash element while traversing via values coredumped
by ioannis (Abbot) on Jun 08, 2006 at 09:03 UTC

    It is not very difficult if you enable warnings; it says "Use of freed value in iteration". Note that $a{b} is deleted in both iterations, yet on on of the iterations it tries to derefence its value of the key (the value is already freed, along with the key.) The program decides to exit with die() and does not crash.

Re: delete hash element while traversing via values coredumped
by gellyfish (Monsignor) on Jun 08, 2006 at 08:56 UTC

    It shouldn't coredump at all, and it doesn't for me. If you have a perl older than 5.8.1 then I would suggest you upgrade. If you have a newer one then I would use perlbug to submit this as a bug report.

    /J\

Re: delete hash element while traversing via values coredumped
by davido (Cardinal) on Jun 08, 2006 at 16:15 UTC

    Who is giving you these snippets? Is someone putting you up to this?

    You've been around awhile here, so I'm sure by now you've read perlfaq4. But just in case, here's an excerpt from it that seems relevant and fairly direct:

    What happens if I add or remove keys from a hash while iterating over it?

    (contributed by brian d foy)

    The easy answer is "Don't do that!"

    If you iterate through the hash with each(), you can delete the key most recently returned without worrying about it. If you delete or add other keys, the iterator may skip or double up on them since perl may rearrange the hash table. See the entry for each() in perlfunc.

    Please tell us which version of Perl is segfaulting with the code snippet you provided. I am anxious to hear the answer.


    Dave

Re: delete hash element while traversing via values coredumped
by holli (Abbot) on Jun 08, 2006 at 09:00 UTC
    It doesn't coredump for me (ActivePerl 5.8.8.817) but i get
    Use of freed value in iteration at dump.pl line 8.
    and the script dies.


    holli, /regexed monk/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-04-18 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found