in reply to Re: Need to get the intersect of hashes
in thread Need to get the intersect of hashes

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).
  • Comment on Re^2: Need to get the intersect of hashes

Replies are listed 'Best First'.
Re^3: Need to get the intersect of hashes
by moritz (Cardinal) on May 15, 2008 at 08:15 UTC
    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?
Re^3: Need to get the intersect of hashes
by grizzley (Chaplain) on May 15, 2008 at 08:12 UTC
    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?
        Could the problem be that I have more than one value in some of the keys?

        Yes.

        foreach my $intersect_file(@intersect_keys) { my $exec = $system_file_data{$intersect_file}; my @values = ref $exec ? @$exec : ($exec); print "$_\n" for @values; }

        See perlreftut and perldsc for more information about nested structures.

        Bingo! I thought so :) It is not a hash - it is hash of arrays and that's the place where you have to get familiar with references. $system_file_data{$intersect_file} gives you in return reference to an array, so you must do:
        $array_ref = $system_file_data{$intersect_file}; @array = @$array_ref; print @array; # or print $array[0];
        Check the other hash, it should be accessible in similar way, too.

        Could the problem be that I have more than one value in some of the keys?

        No, because it's impossible for a hash to have more (or less) than one value per key. Your values are references to arrays. The referenced arrays may containing any number of values, but the values of your hash are simply references.

        Once you fetch the reference, you may iterate over the referenced array:

        foreach my $intersect_file (@intersect_keys) { my $execs = $system_file_data{$intersect_file}; # Array ref foreach my $exec (@$exec) { print("$exec\n"); } }

        It could also be written as the following, but I prefer the former:

        foreach my $intersect_file (@intersect_keys) { foreach my $exec (@{ $system_file_data{$intersect_file} }) { print("$exec\n"); } }