Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Comparison of hashes

Expand Messages
  • merlyn@stonehenge.com
    ... Sreeram Hi, Sreeram I have one very basic question: Sreeram How do I compare 2 hashes and know whether they are same or not? You need to
    Message 1 of 7 , Dec 12, 2005
    • 0 Attachment
      >>>>> "Sreeram" == Sreeram B S <sreeramabsc@...> writes:

      Sreeram> Hi,
      Sreeram> I have one very basic question:

      Sreeram> How do I compare 2 hashes and know whether they are same or not?

      You need to walk them, element by element. See Test::Deep for examples
      of how to do that.

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
      See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    • Charles K. Clarkson
      ... A hash is made up of keys and values. You would need to compare each key value pair to determine equality. Since perl will not guaranty the order of the
      Message 2 of 7 , Dec 12, 2005
      • 0 Attachment
        Sreeram B S <> wrote:

        : How do I compare 2 hashes and know whether they are same or
        : not?

        A hash is made up of keys and values. You would need to
        compare each key value pair to determine equality. Since perl will
        not guaranty the order of the keys in a hash, flattening the hash
        is not guaranteed to work the same way twice.


        : I tried the following code:
        :
        : 1 #!/usr/bin/perl
        : 2
        : 3 my %hash1 = (
        : 4 "first" => 1,
        : 5 "second" => 2,
        : 6 );
        : 7
        : 8 my %hash2 = (
        : 9 "first" => 1,
        : 10 "second" => 2,
        : 11 );
        : 12
        : 13 if ( %hash1 == %hash2 )

        When you made this comparison you were using the scalar values
        of the %hash1 and %hash2. As long as they both have the same
        number of keys, these should be equal. I didn't look it up in the
        docs, but IIRC, scalar %hash1 will return the number of buckets
        used out of the number of buckets allocated for this hash.

        We can test this by changing line 13 through 15 to:

        printf "hash1: %s\nhash2: %s\n", scalar %hash1, scalar %hash2;

        __END__


        : Kindly suggest me the right approach of comparing two hashes
        : and knowing whether they are same.

        The most straight forward way to compare two simple hashes
        that you provided would be to break them into two arrays and
        compare the arrays. Something like this might do.

        my @sorted_hash1_values = sort values %hash1;
        my @sorted_hash2_values = sort values %hash2;

        my @sorted_hash1_keys = sort keys %hash1;
        my @sorted_hash2_keys = sort keys %hash2;

        if ( "@sorted_hash1_values" eq "@sorted_hash2_values"
        and "@sorted_hash1_keys" eq "@sorted_hash2_keys" ) {

        print "Two hashes are equivalent\n";

        } else {
        print "Two hashes are not equivalent\n";
        }

        The problem with this solution is that many hashes are not
        this simple. To compare two complex data structures, we need a more
        robust solution.

        #!/usr/bin/perl

        use strict;
        use warnings;

        use Data::Compare 'Compare';

        my %hash1 = (
        "first" => 1,
        "second" => 2,
        );

        my %hash2 = (
        "first" => 1,
        "second" => 2,
        );

        if ( Compare( \%hash1, \%hash2 ) ) {
        print "Two hashes are equivalent\n";

        } else {
        print "Two hashes are not equivalent\n";
        }

        __END__

        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        254 968-8328
      • Charles K. Clarkson
        ... A hash is made up of keys and values. You would need to compare each key value pair to determine equality. Since perl will not guaranty the order of the
        Message 3 of 7 , Dec 12, 2005
        • 0 Attachment
          Sreeram B S <> wrote:

          : How do I compare 2 hashes and know whether they are same or
          : not?

          A hash is made up of keys and values. You would need to
          compare each key value pair to determine equality. Since perl will
          not guaranty the order of the keys in a hash, flattening the hash
          is not guaranteed to work the same way twice.


          : I tried the following code:
          :
          : 1 #!/usr/bin/perl
          : 2
          : 3 my %hash1 = (
          : 4 "first" => 1,
          : 5 "second" => 2,
          : 6 );
          : 7
          : 8 my %hash2 = (
          : 9 "first" => 1,
          : 10 "second" => 2,
          : 11 );
          : 12
          : 13 if ( %hash1 == %hash2 )

          When you made this comparison you were using the scalar values
          of the %hash1 and %hash2. As long as they both have the same
          number of keys, these should be equal. I didn't look it up in the
          docs, but IIRC, scalar %hash1 will return the number of buckets
          used out of the number of buckets allocated for this hash.

          We can test this by changing line 13 through 15 to:

          printf "hash1: %s\nhash2: %s\n", scalar %hash1, scalar %hash2;

          __END__


          : Kindly suggest me the right approach of comparing two hashes
          : and knowing whether they are same.

          The most straight forward way to compare two simple hashes
          that you provided would be to break them into two arrays and
          compare the arrays. Something like this might do.

          my @sorted_hash1_values = sort values %hash1;
          my @sorted_hash2_values = sort values %hash2;

          my @sorted_hash1_keys = sort keys %hash1;
          my @sorted_hash2_keys = sort keys %hash2;

          if ( "@sorted_hash1_values" eq "@sorted_hash2_values"
          and "@sorted_hash1_keys" eq "@sorted_hash2_keys" ) {

          print "Two hashes are equivalent\n";

          } else {
          print "Two hashes are not equivalent\n";
          }

          The problem with this solution is that many hashes are not
          this simple. To compare two complex data structures, we need a more
          robust solution.

          #!/usr/bin/perl

          use strict;
          use warnings;

          use Data::Compare 'Compare';

          my %hash1 = (
          "first" => 1,
          "second" => 2,
          );

          my %hash2 = (
          "first" => 1,
          "second" => 2,
          );

          if ( Compare( \%hash1, \%hash2 ) ) {
          print "Two hashes are equivalent\n";

          } else {
          print "Two hashes are not equivalent\n";
          }

          __END__

          HTH,

          Charles K. Clarkson
          --
          Mobile Homes Specialist
          254 968-8328
        • pearl16v
          Just use a module. http://search.cpan.org/~zenspider/Struct-Compare-1.0.1/Compare.pm ... Two hashes are same . ... second to 3 and re-executed the code.
          Message 4 of 7 , Dec 12, 2005
          • 0 Attachment
            Just use a module.

            http://search.cpan.org/~zenspider/Struct-Compare-1.0.1/Compare.pm


            --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
            wrote:
            >
            > Hi,
            > I have one very basic question:
            >
            > How do I compare 2 hashes and know whether they are same or not?
            >
            > I tried the following code:
            >
            > 1 #!/usr/bin/perl
            > 2
            > 3 my %hash1 = (
            > 4 "first" => 1,
            > 5 "second" => 2,
            > 6 );
            > 7
            > 8 my %hash2 = (
            > 9 "first" => 1,
            > 10 "second" => 2,
            > 11 );
            > 12
            > 13 if ( %hash1 == %hash2 )
            > 14 {
            > 15 print ("Two hashes are same\n");
            > 16 } else {
            > 17 print ("Two hashes are not equal\n");
            > 18 }
            > 19
            >
            > I tried this and upon execution, I obtained the result as :
            "Two hashes are same".
            > Next, in my second attempt, at line 10, I changed the value of
            "second" to 3 and re-executed the code. But, again I obtained the
            result as : "Two hashes are same".
            >
            > Kindly suggest me the right approach of comparing two hashes
            and knowing whether they are same.
            >
            > Thanks,
            > Sreeram
            >
            >
            >
            >
            > ---------------------------------
            > Yahoo! Shopping
            > Find Great Deals on Holiday Gifts at Yahoo! Shopping
            >
            > [Non-text portions of this message have been removed]
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.