Click to See Complete Forum and Search --> : Hash with 2 identical keys


user
08-24-2006, 03:02 PM
Hi,
I have a hash where I have 2 keys with the same names but different values. When I print a hash (by keys or using each) the second instant of the key dissapears. So I can't print 2 key-value pairs where the keys are the same.
Can someone please help? I need the ability to print it.

Thank you.

CyCo
08-24-2006, 07:36 PM
Hash with 2 identical keysEach key in a hash must be unique.

It seems that if all you really need are multiple values for some of the keys, you could use the logic in the example below:my %hash = (
key1 => ['value1'],
key2 => ['value2'],
key3 => ['value3'],
key4 => [qw/value4 value5 value6/],
);

for (sort keys %hash) {
print "$_ => ", join(', ', @{$hash{$_}}), "\n";
}