How to compare two Hashes so to return true if both Hashes have same keys/values?
Date : March 29 2020, 07:55 AM
Hope that helps I am using Ruby on Rails 3.2.2 and Ruby 1.9.3. I would like to compare two Hashes (A and B) so to return true if a Hash (A) include all keys/values of the other Hash (B). class Hash
def >=(b)
eq = true
b.each { |k, v| eq &= !(self.include? k) ? false : ( ( ((self[k]&&v).is_a? Hash) && !((v||self[k]).empty?) ) ? self[k]>=v : true)}
return eq
end
end
params = { "action"=>"...", "controller"=>"...", "key_param1"=>"value_param1", "key_param2"=>"value_param2", "key_param3"=>"value_param3" }
my_hash1 = { "key_param1"=>"value_param1", "key_param2"=>"value_param2" }
my_hash2 = { "key_param4"=>"value_param4", "key_param1"=>"value_param1" }
my_hash3 = {}
p params >= my_hash1 #true
p params >= my_hash2 #false
p params >= my_hash3 #true
b = {1 => {2 => {} }, 4 => {} }
a = {1 => {2 => {3 => {} }}, 4 => {}, 5 => "123" }
p a >= b #true
p b >= a #false
|
How do I compare two arrays filled with hashes and return the hashes that are not present in the second?
Date : March 29 2020, 07:55 AM
wish helps you Array#- would work: array_a = [{'key' => 'a'}, {'key' => 'b'}, {'key' => 'c'}, {'key' => 'd'}]
array_b = [{'key' => 'a'}, {'key' => 'b'}, {'key' => 'd'}]
array_a - array_b
#=> [{"key"=>"c"}]
|
How do I compare two hashes containing ~25000 hashes?
Date : March 29 2020, 07:55 AM
this will help This will return all the keys that were changed (i.e. created, removed or updated): (old_hash.keys | new_hash.keys).select { |k| old_hash[k] != new_hash[k] }
keys = (old_hash.keys | new_hash.keys)
new_keys = keys.select { |k| old_hash[k].nil? }
deleted_keys = keys.select { |k| new_hash[k].nil? }
modified_keys = keys.select { |k| old_hash[k] != new_hash[k] }
unchanged_keys = keys - (new_keys | deleted_keys | modified_keys)
|
How to compare 2 hashes and findout how many nested hashes have changed
Date : March 29 2020, 07:55 AM
wish of those help From comparing one hash to another I need to get the number of nested hashes that have changed , I think you can do hash1.to_a - hash2.to_a to obtain the differences
|
how to compare hashes of hashes perl
Date : March 29 2020, 07:55 AM
help you fix your problem You need to iterate one of them and check for the existence of each key. Here is a verbose implementation. use strict;
use warnings;
my %foo = ( a => 1, b => 2, c => 3, x => 7 );
my %bar = ( x => 7, y => 8, z => 9 );
foreach my $key ( keys %bar ) {
CORE::say $key if exists $foo{$key} && $bar{$key} eq $foo{$key};
}
use strict;
use warnings;
use Test::More;
my @test_cases = (
{
'name' => 'all keys match',
'th' => {
surfacecharge => {
87 => 'negatively charged',
},
},
'cd' => {
surfacecharge => {
87 => 'negatively charged',
},
},
'true positives' => { surfacecharge => 1, }, # here
'false negatives' => { surfacecharge => 0, }, # but this one comes out undef
},
{
'name' => 'key matches, value does not',
'th' => {
surfacecharge => {
87 => 'negatively charged',
},
},
'cd' => {
surfacecharge => {
87 => 'positively charged',
},
},
'true positives' => { surfacecharge => 0, },
'false negatives' => { surfacecharge => 1, },
},
{
'name' => 'two matching keys',
'th' => {
surfacecharge => {
87 => 'negatively charged',
88 => 'chronically tired',
},
},
'cd' => {
surfacecharge => {
87 => 'negatively charged',
88 => 'chronically tired',
},
},
'true positives' => { surfacecharge => 2, },
'false negatives' => { surfacecharge => 0, },
},
{
'name' => 'two/zero, one/one',
'th' => {
surfacecharge => {
87 => 'negatively charged',
88 => 'chronically tired',
},
areasurcharge => {
1 => 'stuff',
2 => 'goo',
},
},
'cd' => {
surfacecharge => {
87 => 'negatively charged',
88 => 'chronically tired',
},
areasurcharge => {
1 => 'stuff',
0 => 'do not want',
},
},
'true positives' => { surfacecharge => 2, areasurcharge => 1, },
'false negatives' => { surfacecharge => 0, areasurcharge => 1, },
},
);
foreach my $test (@test_cases) {
my ( $true_positives, $false_negatives ) = determine_accuracy( $test->{th}, $test->{cd} );
is_deeply $true_positives, $test->{'true positives'}, "$test->{name}: true positives";
is_deeply $false_negatives, $test->{'false negatives'}, "$test->{name}: false negatives";
}
done_testing;
sub determine_accuracy {
my ( $instancesTH, $instancesCD ) = @_;
my $tp;
my $fn;
foreach my $file ( keys %{$instancesTH} ) {
$tp->{$file} = 0;
$fn->{$file} = 0;
foreach my $pos ( keys %{ $instancesTH->{$file} } ) {
if ( exists $instancesCD->{$file}->{$pos}
&& $instancesCD->{$file}->{$pos} eq $instancesTH->{$file}->{$pos} )
{
$tp->{$file}++;
}
else {
$fn->{$file}++;
}
}
}
return $tp, $fn;
}
|