2
votes

What I am attempting to do:

Store files and their md5 hash values into a hash, changing the hash when files are added or deleted.

So far I am able to store the md5 hashes and update the hash when files are added. However I am not sure how to delete the keys for files that are removed

My Approach right now is to:

turn array into a hash to compare

my %files = map { $_ => 1 } @files;

check to see if they are the same

if (%files ~~  %hash).. same.. else... different

I'm not sure how to implement this further.. here is my thinking:

Delete the key in the hash that no longer has a file(pseudo code)

Where exists $hash{$_} && !exists $files{$_} delete $hash{$_}
2

2 Answers

4
votes

Just loop over one, and check if exists and delete...

foreach my $k ( keys %file ) {
  delete $hash{$k} if exists $hash{$k};
}

Also, because of internals when using an hash for this purpose you're better off assigning undef than 1 to it. Reasons withheld for simplicity.

my %files = map { $_ => undef } @files;

I'm not sure if the conditional makes it faster or slower -- it probably doesn't matter.

2
votes

You don't need to create hashes to compare lists. Have a look at List::Compare. In particular, the get_unique() and get_complement() methods will identify items which only appear in one of your two lists.