in reply to Re^4: Iterating over hash while deleting elements
in thread Iterating over an hash while removing keys
I guess then:
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.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(); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Iterating over hash while deleting elements (Best Practice)
by LanX (Saint) on Feb 07, 2020 at 11:52 UTC | |
by Eily (Monsignor) on Feb 07, 2020 at 14:11 UTC | |
by LanX (Saint) on Feb 07, 2020 at 14:23 UTC |
In Section
Seekers of Perl Wisdom