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

the_slycer has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I have some code that is comparing the contents of an array vs a hash, the basic idea is that if the array contains some new data to add it to the hash, AND if the hash contains something that is not in the array, to remove it.

The below "works" but to me appears terribly inneficient and kind of ugly, looking for a better way of doing this:
my @newfiles = ("new_one.txt", "old_one.txt", "old_two.txt", "new_two.txt"); my %oldfiles; $oldfiles{"old_one.txt"}="this is the old one"; $oldfiles{"old_two.txt"}="this is the old two"; $oldfiles{"old_three.txt"}="this one should be deleted"; $oldfiles{"new_two.txt"}="this one has been added already"; my %newfiles; foreach (@newfiles){ $newfiles{$_}++; next if exists $oldfiles{$_}; print "have to add $_\n"; } foreach (keys %oldfiles){ next if exists $newfiles{$_}; print "have to remove $_\n"; }
Thanks..