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

Hi all, i m new to Perl world. Can any one please help. I have 3 hashes, i need to compare these 3 hashes with each other and need to find out if any of the Key matches
output should be : comparing %hash1 with %hash 2 : found 1 match which is D comparing %hash1 with %hash 3 : found 1 match which is D comparing %hash2 with %hash 3 : found 1 match which is D and J my %hash1 = ( A => 1, B => 2, C => 3, D => 4, ); my %hash2 = ( D=>1 E =>2, F => 3, G => 4, H => 5, I => 6, J=>7 ); my %hash3 = ( J=>1 K =>2, L => 3, M => 4, N => 5, O => 6, P=>7 Q =>8, R =>9, S =>10, T => 11, U =>12, D=>13, );

Replies are listed 'Best First'.
Re: Comparing three Hashes
by jethro (Monsignor) on Apr 20, 2011 at 11:45 UTC

    If you split the problem into its parts it gets easier: Create a subroutine that compares two hashes. Then call the subroutine with all three combinations of hashes.

    To create the subroutine that compares two hashes, just use a loop that steps through the keys of one hash and checks if they are in the second hash. Hint: The function keys() will give you a list of all keys of a hash

    If you have problems, post your script here and we help you debug it

Re: Comparing three Hashes
by moritz (Cardinal) on Apr 20, 2011 at 11:48 UTC

    What have you tried?

    You should start by breaking up the problem into smaller steps, for example

    • Compare just two hashes
    • When you're satisfied with the code for it, and it does what you want, turn it into a subroutine
    • Then it's just a matter of calling this sub three times, once with hashes 1 and 2, once with 1 and 3, and once with 2 and 3
    • profit!

    perlintro should get you started, perlfaq4 could be valuable too.

Re: Comparing three Hashes
by LanX (Saint) on Apr 20, 2011 at 11:49 UTC
    Unfortunately your post is not readable, please update and insert code tags <code>...</code>

    Cheers Rolf

Re: Comparing three Hashes
by InfiniteSilence (Curate) on Apr 20, 2011 at 13:24 UTC

    This sounds like homework to me and I'll answer it as if that is the case.

    What you are trying to do, if I have it correct, is to take N hashes and establish where they share the same key values.

    If you ignore the fact that they are hashes for a minute you can effectively separate Perl syntax from the actual problem itself. So you actually have two questions, how do you loosely define a data structure that has the information you want that you can use and, afterward, to write it in Perl.

    Here I'll show one way to do it which is pretty (purposefully) verbose but works:

    sub dump_hash_maker { my ($ltrStart, $size) = @_; my $stop = chr (ord($ltrStart)+$size); my @letters; push @letters, $_ for ($ltrStart..$stop); my %hash = map {(shift @letters)=>$_} (1..$size) ; return %hash; } my %hash1 = dump_hash_maker('A',4); my %hash2 = dump_hash_maker('D',7); my %hash3 = dump_hash_maker('J',13); my @allkeys = (); my (%intermediate); push @allkeys,(keys %hash1); push @allkeys,(keys %hash2); push @allkeys,(keys %hash3); for(@allkeys) { $intermediate{$_}++; } for(keys %intermediate) {print $_ if $intermediate{$_} > 1}; # prints JD

    You'll see that I dumped all of the individual keys into one big array and then folded that into a hash by incrementing the number of times it occurs. There are much smaller examples of this in Perl books. Another approach would have been to build a larger data structure and then write a (recursive?) function that would return nodes with a depth > 1.

    Celebrate Intellectual Diversity

Re: Comparing three Hashes
by Marshall (Canon) on Apr 20, 2011 at 19:47 UTC
    Yes, this looks like homework.

    From your example input/output, it appears that you are only interested in whether a particular key occurs in common between the hashes (the value of those keys doesn't matter). Consider:

    #!/usr/bin/perl -w use strict; my %hash1 = ( A => 1, B => 2, C => 3, D => 4, ); my %hash2 = ( D=>1, E =>2, F => 3, G => 4, H => 5, I => 6, J=>7 ); my %hash3 = ( J=>1, K =>2, L => 3, M => 4, N => 5, O => 6, P=>7, Q =>8, R =>9, S =>10, T => 11, U =>12, D=>13, ); foreach my $key ( keys(%hash1), keys(%hash2), keys(%hash3) ) { print "$key"; } print "\n"; #prints:ADCBHFJDIGESOJTNPKQMDRLU
    One property of a hash is that the key is "unique" - it can only occur once in a particular hash.
    Hashes are good for "counting number of occurrences of things".
    In the above, since 'D' occurs 3 times, it is possible to conclude that they key 'D' appears in common with all 3 hashes.
    I will leave it to you as to how to use this example in your homework.