Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 4.57 What happens if I add or remove keys from a hash while iterating over it?

Reply
Thread Tools

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

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      01-10-2011
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.57: 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.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
OK to delete hash pairs while iterating through it? Jerry Krinock Perl Misc 7 09-12-2008 08:00 PM
Iterating over a hash of hash of hashes Steven Hirsch Ruby 0 08-19-2008 12:34 AM
What happens when type conversion between signed and unsigned happens? NM C++ 6 09-20-2006 05:39 PM
hash in hash, iterating the members Depili Ruby 5 01-13-2006 03:06 AM



Advertisments