http://qs321.pair.com?node_id=11112507


in reply to Re^4: Iterating over hash while deleting elements
in thread Iterating over an hash while removing keys

I guess then:

sub delete_first(\%) { my $hash = shift; keys %$hash; my ($key, $value) = each %$hash; delete $hash->{$key}; wantarray ? ($key, $value) : $key; } while (my $key = delete_first %hash) { do_stuff(); }
Would be safer. At least delete_first doesn't depend on the current state of the hash's iterator, and the iterator (each) is still valid right after calling it.