Gorgarian has asked for the wisdom of the Perl Monks concerning the following question:
Hi all in the monastry -- hope you are not all sworn to silence during the festive season.
I have a small challenge, being fairly new to perl. I wish to step through a hash with
while ( ($key, $value) = each %hash ){ }
but then modify the hash by deleting key,value pairs from the hash after some processing if they meet a certain condition, with the while loop in progress.
I have this nagging worry that modifiying the contents of the hash on the fly will stuff up the orderly progression of the while loop and that some records may be missed.
Can anyone with a deeper knowledge of perl advise on this?
A workaround would be to establish a companion array
@array = keys %hash; foreach $key (@array){ # Access the hash, and do the deed including any hash # key, value deletions. }
but this doubles the memory requirements, and my files are huge. I would like to avoid this if it is not necessary.
Looking forward to hearing from you. My first post.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Stepping through a hash while modifiying its content
by Anonymous Monk on Dec 28, 2012 at 04:58 UTC | |
by Gorgarian (Initiate) on Dec 28, 2012 at 12:13 UTC | |
|
Re: Stepping through a hash while modifiying its content
by Anonymous Monk on Dec 28, 2012 at 05:02 UTC | |
by Gorgarian (Initiate) on Dec 28, 2012 at 12:19 UTC | |
|
Re: Stepping through a hash while modifiying its content
by karlgoethebier (Abbot) on Dec 28, 2012 at 14:43 UTC |