Steffan Harris
Steffan Harris

Reputation: 9326

Can a Hash have duplicate keys or values

Can a Hash have duplicate keys or values?

Upvotes: 11

Views: 39812

Answers (6)

MS-AccessUnleashed
MS-AccessUnleashed

Reputation: 1

Yes a hash can have duplicate keys as I demonstrate below...

Key example: BirthDate|LastNameFirst4Chars|FirstNameInitial|IncNbr "1959-12-19|Will|K|1" ... "1959-12-19|Will|K|74".
Note: This might be a useful Key for record look ups if someone did not remember their Social Security Nbr

   #-- CODE SNIPPET:
   @Offsets=();     #-- we will build an array of Flat File record "byte offsets" to random access
                    #-- for all records matching this ALT KEY with DUPS       
   for ($i=1; $i<=99; $i++) {
       $KEY=$BirthDate . "|" . $LastNameFirst4Chars . "|" . $FirstNameInitial . "|" . $i; 
       if (exists $Hash{$KEY}) {
           push @Offsets, $Hash{$KEY};  #-- add another hash VALUE to the end of the array
       }
   } 

Upvotes: -1

Mayank
Mayank

Reputation: 231

Please try and run this code, it executes without errors. I hope this is what you were asking!

#!/usr/bin/perl

use strict;
use warnings;

my %hash = ('a' => 1, 'a' => 2, 'b' => 4 );

print values %hash, "\n\n";
print keys %hash, "\n\n";

Upvotes: 3

czubatka
czubatka

Reputation: 61

You can try to use Hash::MultiKey module from CPAN.

(I used Data::Dumper to show how hash is exactly looks - it is not necessary here)

use Data::Dumper;
use Hash::MultiKey;

tie my %multi_hash, 'Hash::MultiKey';

$multi_hash{['foo', 'foo', 'baz']} = "some_data";

for (keys %multi_hash) {
    print @$_,"\n";
};

print Dumper\%multi_hash;

And the output shoud be () :

foofoobaz
$VAR1 = {
          'ARRAY(0x98b6978)' => 'some_data'
        };

So technically speaking Hash::MultiKey let you create reference as a hash key.

Upvotes: 2

ikegami
ikegami

Reputation: 386311

For both hashes and arrays, only one scalar can be stored at a given key. ("Keys are unique.") If they weren't, you couldn't do

$h{a} = 1;
$h{a} = 2;
$val = $h{a};  # 2

$a[4] = 1;
$a[4] = 2;
$val = $a[4];  # 2

If you wanted to associate multiple values with a key, you could place a reference to an array (or hash) at that key, and add the value to that array (or hash).

for my $n (4,5,6,10) {
    if ($n % 2) {
        push @{ $nums{odd} }, $n;
    } else {
        push @{ $nums{even} }, $n;
    }
}

say join ', ', @{ $nums{even} };

See perllol for more on this.

As for values, multiple elements can have the same value in both hashes and arrays.

$counts{a} = 3;
$counts{b} = 3;

$counts[5] = 3;
$counts[6] = 3;

Upvotes: 11

user166390
user166390

Reputation:

Assuming talking about a "%hash"

Then:

  • Duplicate keys not allowed.
  • Duplicate values allowed.

This is easy to reason about because it is a mapping of a particular Key to a particular Value where the Value plays no part in the look-up and is thus independent upon other Values.

Upvotes: 6

Alex Gitelman
Alex Gitelman

Reputation: 24722

it can have duplicate values but not keys.

Upvotes: 17

Related Questions