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

i have 2 compares the 2 hashes their values.If they are not equal i ahve to keep in one separate hash3. from the result hash i need to find out only values for specific keys in array.Can you please help me on this. suppose if the result hash is my %hash1 = ( 'key1' => '7', 'another_key' => '2', 'the third' => '8',); my %hash2 = ( 'key1' => '7', 'another_key' => '2', 'the third' => "10",); so %hash3 should be like this after comparing these 2 hashes. my %hash3 = ( 'key1' => '7', 'another_key' => '2', ); i have another array below like this @array={key1', 'data2'}; so finally it should print the only value o/p---key1 and 7

  • Comment on How to compares 2 hashes and keep it in 3rd hash after that seggregate from array

Replies are listed 'Best First'.
Re: How to compares 2 hashes and keep it in 3rd hash after that seggregate from array
by pme (Monsignor) on Feb 08, 2015 at 11:35 UTC
    Please put <code> </code> tags around your code and data.

    Update

    You may find this useful if I understand your question correctly:

    use strict; use Data::Dumper; my %hash1 = ( 'key1' => '7', 'another_key' => '2', 'the third' => '8', ); my %hash2 = ( 'key1' => '7', 'another_key' => '2', 'the third' => "10", ); my @array = ('key1', 'data2'); # turning array into hash to do lookup inherently my %array_hash = map {$_, 1} @array; my %hash3; foreach (keys %hash1) { if ($hash1{$_} == $hash2{$_} && exists $array_hash{$_}) { $hash3{$_} = $hash1{$_}; } } print Dumper(\%hash3) . "\n";