Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #313 Map equality

Set b to true if the maps m and n have the same key/value entries, false otherwise.

use Data::Compare;
$b = Compare( \%hash1, \%hash2 );
sub hcmp_numeric {
    my ($h, $g) = @_;

    my $hc = keys %$h;
    my $gc = keys %$g;

    return 0 unless $hc == $gc;
    return 0 unless $hc == grep { exists $g->{$_} } keys %$h;
    die 'non-scalar value detected' 
        if 0 < grep { ref $h->{$_} or ref $g->{$_} } keys %$h;
    return 0 unless $hc == grep { $h->{$_} == $g->{$_} } keys %$h;
    return 1;
}
#include <map>
b = m == n;
import "maps"
b := maps.Equal(m, n)
boolean b = m.equals(n);
b = m == n
b = m == n

New implementation...
< >
programming-idioms.org