I have fairly large hash (some 10M keys) and I would like to delete some elements from it.
I usually don\'t like to use delete or splice, a
I recommend doing two passes because it's more robust. Hash order is effectively random, so there are no guarantees that you'll see the "primary" keys before the related ones. For example, if should_be_deleted() only detects the primary keys that aren't wanted and the related ones are calculated, you could end up processing unwanted data. A two-pass approach avoids this issue.
my @unwanted;
foreach my $key (keys %hash) {
if (should_be_deleted($key)) {
push @unwanted, $key;
# push any related keys onto @unwanted
}
}
delete @hash{@unwanted};
foreach my $key (keys %hash) {
# do something
}