I am pretty new to Perl and need to accomplish a task quickly. Any help is appreciated!
I have two hash of arrays as follows:
Hash 1
-------
w: ['A','B','C']
e: ['P','Q','R']
Hash 2
-------
w:['A','B','C']
e:['P','Q','O']
r:['S','T']
Context:
-
I want to find the differece in values for same keys(e.g Hash 1 does not have
value ‘O’ from Hash 2 for the same key ‘e’. -
Find the difference in keys. (e.g ‘r’ is not present in hash 1.
I put together some code but it checks the exact value of the complete line from two hashes. For example If I have ‘A’,’B’,’C’ in hash 1 for key w and ‘B’, ‘C’,’A’ in hash 2 for same key if flags a difference. I want to compare value by value-
Following code compares two hash os arrays. So from the above example A,B,C from hash 1 is not equal to B , A,C from hash 2. However I want to check the existence of the individual item say A in and not worry about the order.
for ( keys %hash2 )
{
unless ( exists $hash1{$_} ) # Checks for mismatches in keys
{
print "$_: Key mismatch $_ received \n";
next;
}
if ( $hash2{$_} eq $hash1{$_} ) #Compares two lines exactly
{
print "$_: No mismatch \n";
}
else
{
print "$_: Value mismatch for key $_ \n"; #Difference in Value
}
}
If you don’t care about orders, just compare the ordered set of values:
your code:
Should be:
On the other hand, if you want to compare memberships of arrays, simply turn array into a hashref: