I basically want to do this:
foreach my $key (keys $hash_ref) {
Do stuff with my $key and $hash_ref
# Delete the key from the hash
delete $hash_ref->{$key};
}
Is it safe? And why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re not iterating over the hash, you’re iterating over the list of keys returned by
keysbefore you even started looping. Keep in mind thatis roughly the same as
Deleting from the hash causes no problem whatsoever.
each, on the other, does iterate over a hash. Each time it’s called,eachreturns a different element. Yet, it’s still safe todeletethe current element!