On Aug 2, 3:13 am, [email protected] (Sharan Basappa) wrote:
> On Mon, Aug 2, 2010 at 3:36 PM, Uri Guttman <[email protected]> wrote:
> >>>>>> "SB" == Sharan Basappa <[email protected]> writes:
>
UG> > SB> Is there any restriction that the keys in perl should be
unique?
UG> > SB> If not, are there any special considerations that need to
be kept
UG> > SB> in mind while accessing the elements?
UG> > well, think about it first. what would happen if a hash allowed
UG> > duplicate keys? how would you know which associated value was
attached
UG> > to the duplicate key? the whole point of hashes is to index by a
UG> > string. requiring unique keys is what makes it a hash. in a
similar vein
UG> > what would happen if you could index an array multiple times
with the
UG> > same integer? it makes no sense. same for hashes.
>
SB> I figured this out once I ran the code
SB> where I got this doubt.
SB> The reason why I raised this question is that languages like C++
do
SB> allow hashes arrays
SB> that can have different values but same key. Thats the reason for
the
SB> second part of my
SB> question.
DB_FILE's module implements a 'hash-like' type BTREE
based on the Berkeley DB API which can store/retrieve
duplicate keys.
See: http://search.cpan.org/~pmqs/DB_File-1.820/DB_File.pm
>From above doc:
$h{'Wall'} = 'Larry' ;
$h{'Wall'} = 'Brick' ; # Note the duplicate key
$h{'Wall'} = 'Brick' ; # Note the duplicate key and value
...
for ( ... } { print "$key -> $value\n" }
---> Wall -> Brick
---> Wall -> Brick
----> Wall -> Larry
--
Charles DeRykus
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/