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

Remove duplicate keys from a hash

Expand Messages
  • Md. Musfique Anwar
    Hi, I want to know is there any way to detect duplicate keys in hash. I have a CDB database and from that I create hash and the hash contains duplicate keys. I
    Message 1 of 2 , Nov 28, 2012
    View Source
    • 0 Attachment
      Hi,

      I want to know is there any way to detect duplicate keys in hash.

      I have a CDB database and from that I create hash and the hash contains
      duplicate keys. I want to remove duplicate keys from hash. Below I gave my
      sample perl code:

      --------------------------------------------

      my @arr = (1,2,1,4);

      my $db = new CDB_File ("test_cdb.cdb", "test_cdb.$$") or die "$!";

      for(my $i=0; $i < 4; $i++){

      # print "$arr[$i]\n";
      $db->insert($arr[$i], $arr[$i] . "=" . 10);
      }

      $db->finish;

      tie %data, 'CDB_File', "test_cdb.cdb" or die;

      while(my ($k, $v) = each %data){
      print "$k -> $v\n";
      }

      --------------------------------------------

      regards-

      Musfique


      [Non-text portions of this message have been removed]
    • timothy adigun
      Hi, Please see my comments below. On Thu, Nov 29, 2012 at 3:42 AM, Md. Musfique Anwar
      Message 2 of 2 , Nov 28, 2012
      View Source
      • 0 Attachment
        Hi,

        Please see my comments below.

        On Thu, Nov 29, 2012 at 3:42 AM, Md. Musfique Anwar <
        musfique.anwar@...> wrote:

        > **
        >
        >
        > Hi,
        >
        > I want to know is there any way to detect duplicate keys in hash.
        >

        *Hash in Perl don't allow duplicate keys*, though can have duplicate
        values. Except probably you are using cpan module Hash::MultiKey module.
        Which makes that possible, using arrayrefs of strings as keys, which I
        don't think that is what you are doing here.

        E.g:
        use warnings;
        use strict;

        my %data = (
        England => 'London',
        France => 'Paris',
        Nigeria => 'Abuja',
        England => 'London',
        );

        while ( my ( $k, $v ) = each %data ) {
        print "$k -> $v\n";
        }

        *OUTPUT:
        *
        France -> Paris
        England -> London # printed only once
        Nigeria -> Abuja

        But if you have this:

        ...
        my %data = (
        England => 'London',
        France => 'Paris',
        Nigeria => 'Abuja',
        'England ' => 'London', ## this is not the same with first hash key
        );

        then you have this:

        *OUTPUT:*
        France -> Paris
        England -> London
        England -> London
        Nigeria -> Abuja

        I will suggest, you look into how you are generating your hash keys and
        values.

        >
        > I have a CDB database and from that I create hash and the hash contains
        > duplicate keys. I want to remove duplicate keys from hash. Below I gave my
        > sample perl code:
        >
        > --------------------------------------------
        >
        > my @arr = (1,2,1,4);
        >
        > my $db = new CDB_File ("test_cdb.cdb", "test_cdb.$$") or die "$!";
        >
        > for(my $i=0; $i < 4; $i++){
        >
        > # print "$arr[$i]\n";
        > $db->insert($arr[$i], $arr[$i] . "=" . 10);
        > }
        >
        > $db->finish;
        >
        > tie %data, 'CDB_File', "test_cdb.cdb" or die;
        >
        > while(my ($k, $v) = each %data){
        > print "$k -> $v\n";
        > }
        >
        > --------------------------------------------
        >
        > regards-
        >
        > Musfique
        >
        > [Non-text portions of this message have been removed]
        >
        >
        >



        --
        Tim


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.