http://www.perlmonks.org?node_id=1005822


in reply to how do I efficiently remove one hash from another?

If you are concerned about looping through the values (as is common and necessary in all the ways to do what you've asked), is there any other way you can build up or maintain the data in the first place? Whenever I find an expensive operation in one of my programs, it's always worth going back to the data representation itself to see if there is a more efficient way of maintaining the data.

For instance, if the hash keys do not change often, and you are more concerned about CPU than memory, separately maintaining %hash1_minus_hash2 might be better. Your "write" operation takes twice as long (still O(1), just with a higher constant), but your O(n) loop is now O(1), which can be a huge win.

Also, if %hash1 is not more than twice as big as %hash2, and you're going to loop through %hash1 for the next step of your algorithm anyway (and you do this at most once per delete cycle), a construct like this might actually be more efficient:

for (keys %hash1) { next if exists $hash2{$_}; # ... your main loop code here }

There are of course many additional strategies that may be a better fit, but perhaps these two will spark an interest to take the problem up a level.

And, it's entirely possible there's nothing else you can do in your case. If that's true, cheers anyway! :-)