use strict; use warnings; use Data::Dumper; sub are_hashes_equal { my ($hash1,$hash2)=@_; return 1 if $hash1==$hash2; my %temp=%$hash2; foreach my $key (keys %$hash1) { return 0 unless exists $temp{$key} and defined($hash1->{$key}) eq defined($temp{$key}) and (defined $temp{$key} ? $temp{$key} eq $hash1->{$ke +y} : 1); delete $temp{$key}; } return !keys(%temp); } my %aa=(uno=>1, due=>2,trio=>undef,quat=>0,cinq=>''); my @b=( \%aa, {uno=>1, due=>2,trio=>undef,quat=>0,cinq=>''}, {uno=>1, due=>2,trio=>0,quat=>undef,cinq=>''}, {uno=>2, due=>2,trio=>undef,quat=>0,cinq=>''}, {uno=>2, due=>2,trio=>undef,quat=>0}, {uno=>2, due=>2,trio=>undef,quat=>0,cinq=>'',sex=>'is fun'}, ); print Data::Dumper->Dump([\%aa],['*aa']); foreach my $test_id (0..$#b) { print Data::Dumper->Dump([$b[$test_id]],['*test'.$test_id]); print "Is ",(are_hashes_equal(\%aa,$b[$test_id]) ? '' : 'not '), "the same as %aa\n\n"; }

produces

%aa = ( 'cinq' => '', 'trio' => undef, 'due' => 2, 'uno' => 1, 'quat' => 0 ); %test0 = ( 'cinq' => '', 'trio' => undef, 'due' => 2, 'uno' => 1, 'quat' => 0 ); Is the same as %aa %test1 = ( 'cinq' => '', 'trio' => undef, 'due' => 2, 'uno' => 1, 'quat' => 0 ); Is the same as %aa %test2 = ( 'cinq' => '', 'trio' => 0, 'due' => 2, 'uno' => 1, 'quat' => undef ); Is not the same as %aa %test3 = ( 'cinq' => '', 'trio' => undef, 'due' => 2, 'uno' => 2, 'quat' => 0 ); Is not the same as %aa %test4 = ( 'trio' => undef, 'due' => 2, 'uno' => 2, 'quat' => 0 ); Is not the same as %aa %test5 = ( 'cinq' => '', 'trio' => undef, 'due' => 2, 'uno' => 2, 'sex' => 'is fun', 'quat' => 0 ); Is not the same as %aa

HTH


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi



In reply to Re: Comparing hashes without sorting the keys by demerphq
in thread Comparing hashes without sorting the keys by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.