FAQ 4.56 What happens if I add or remove keys from a hash while iterating over it?
- From: PerlFAQ Server <brian@xxxxxxxxxxxxxx>
- Date: Sun, 29 Apr 2007 12:03:02 -0700
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.56: 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.
--
Posted via a free Usenet account from http://www.teranews.com
.
- Prev by Date: Re: Samba file access checking?
- Next by Date: Re: CGI module redirect defaults to 302 -- why?
- Previous by thread: I find the perl syntax easier than python
- Next by thread: Where are good places to post resumes?
- Index(es):
Relevant Pages
|