in reply to Need to get the intersect of hashes

Isn't it that you get every key from one hash and check if it exists in the other hash?
@keys_existing_in_both = grep defined $hash2{$_}, keys %hash1;

Replies are listed 'Best First'.
Re^2: Need to get the intersect of hashes
by Anonymous Monk on May 15, 2008 at 16:48 UTC
Re^2: Need to get the intersect of hashes
by jbush82 (Novice) on May 15, 2008 at 07:54 UTC
    Yes, that does give me the intersect of the keys. What I need to do is take each key in the intersect array (@keys_existing_in_both in your example) and act on each value associated with each key. Thats where I'm confused.

    For example, lets say the the array in your example has the element psexec.exe. What I need to do is search the second hash (the one containing the system files) for psexec and then run a system command on each value associated with the psexec.exe key. Take the results of that data (the md5 of the file) and compare it to the other values ins the psexec.exe key in the first hash (known bad data).
      for my $k (@keys_existing_in_both) { my $exec = $hash2{$k}; # do something with $k my $result = md5($k); if ($result ne $hash2{$k}){ print "Hash sum miss match for '$k'!\n"; } }
      (BTW in the general case exists $hash{$key} checks if an key exists in a hash, not defined $hash{$key}.)
        the output I'm getting when trying to use the value of the keys is ARRAY(0x1a1c4ec)ARRAY(0x2bb0d9c). Any ideas?
      You don't need to search in hash. If you have a key, you just retrieve the value connected with key. Can you print both structures, which you have, with help of Data::Dumper and append to your question?
        I've appended the output of one of my hashes (the other is similar, but MUCH longer.. thousands of files). I'm trying to use the following code:
        foreach my $intersect_file(@intersect_keys) { my $exec = $system_file_data{$intersect_file}; print $exec; }
        ... but $exec outputs as ARRAY(0x1a1c4ec)ARRAY(0x2bb0d9c) instead of the values of each key. Could the problem be that I have more than one value in some of the keys?