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

Dear Monks

If i have 2 hashes how can i find the entries that are present in both? If i have hashes a and b, is it also possible to find the entries present in a but not in b?

thanks a lot

Replies are listed 'Best First'.
Re: compare 2 hashes
by toolic (Bishop) on Feb 26, 2011 at 02:33 UTC
Re: compare 2 hashes
by umasuresh (Hermit) on Feb 26, 2011 at 02:16 UTC
Re: compare 2 hashes
by CountZero (Bishop) on Feb 26, 2011 at 07:36 UTC
    What do you mean by "entries that are present": same keys, same values, same keys and values?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: compare 2 hashes
by GrandFather (Saint) on Feb 26, 2011 at 02:28 UTC

    What have you tried?

    True laziness is hard work
      Was writing this really better than not writing anything?
Re: compare 2 hashes
by stilldreaming (Initiate) on Feb 26, 2011 at 18:25 UTC

    Hello everybody, If you wish to find the common keys you could use something like the following example.

    my %a=('la'=>1, 'ka'=>5, 'pika'=>4); my %b=('pika'=>5, 'kasa'=>4, 'ka'=>44); my %common_keys=(); foreach my $key (keys %a) { if(exists $b{$key}) { $common_keys{$key}='yes'; } } my $the_common_keys_str=join(",", keys %common_keys); print "The common keys are: " . $the_common_keys_str;