has and non unique keys
Folks,
Is there any restriction that the keys in perl should be unique?
If not, are there any special considerations that need to be kept in mind while
accessing the elements?
Regards,
Sharan
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
>>>>> "SB" == Sharan Basappa <sharan.basappa [at] gmail.com> writes:
SB> Is there any restriction that the keys in perl should be unique?
SB> If not, are there any special considerations that need to be kept
SB> in mind while accessing the elements?
well, think about it first. what would happen if a hash allowed
duplicate keys? how would you know which associated value was attached
to the duplicate key? the whole point of hashes is to index by a
string. requiring unique keys is what makes it a hash. in a similar vein
what would happen if you could index an array multiple times with the
same integer? it makes no sense. same for hashes.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
Hello Uri,
Thanks for the explanation. I figured this out once I ran the code
where I got this doubt.
The reason why I raised this question is that languages like C++ do
allow hashes arrays
that can have different values but same key. Thats the reason for the
second part of my
question.
Regards,
Sharan
On Mon, Aug 2, 2010 at 3:36 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
>>>>>> "SB" =3D=3D Sharan Basappa <sharan.basappa [at] gmail.com> writes:
>
> =A0SB> Is there any restriction that the keys in perl should be unique?
> =A0SB> If not, are there any special considerations that need to be kept
> =A0SB> in mind while accessing the elements?
>
> well, think about it first. what would happen if a hash allowed
> duplicate keys? how would you know which associated value was attached
> to the duplicate key? the whole point of hashes is to index by a
> string. requiring unique keys is what makes it a hash. in a similar vein
> what would happen if you could index an array multiple times with the
> same integer? it makes no sense. same for hashes.
>
> uri
>
> --
> Uri Guttman =A0------ =A0uri [at] stemsystems.com =A0-------- =A0http://www.sy=
sarch.com --
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
Hi Sharan,
On Monday 02 August 2010 13:13:19 Sharan Basappa wrote:
> Hello Uri,
>
> Thanks for the explanation. I figured this out once I ran the code
> where I got this doubt.
> The reason why I raised this question is that languages like C++ do
> allow hashes arrays
Please don't call hashes "hashes arrays". They are either "hashes" or
"associative arrays". (Technically, hashes are the name of the implementation
rather than the Abstract Data Type of a Dictionary that matches keys to values
, but we call that in Perl because it's a short and convenient word).
> that can have different values but same key. Thats the reason for the
> second part of my
> question.
Are you talking about the C Standard Template Library (STL)? How does what you
are referring to allow multiple values for the same key? I should note that if
you wish to do something like that in Perl, you can have the key point to a
value that is an array reference, a hash reference or a different data
structure.
Regards,
Shlomi Fish
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Stop Using MSIE - http://www.shlomifish.org/no-ie/
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
Shlomi,
I am not trying to be critical of perl or anything (if that is what you fel=
t).
I am only trying to see if a certain feature exists or not.
The current problem I am working on has duplicate key values and hence
the question.
The STL multipmap library explanation is below:
http://www.cplusplus.com/reference/stl/multimap/
Regards,
Sharan
On Mon, Aug 2, 2010 at 4:18 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> Hi Sharan,
>
> On Monday 02 August 2010 13:13:19 Sharan Basappa wrote:
>> Hello Uri,
>>
>> Thanks for the explanation. I figured this out once I ran the code
>> where I got this doubt.
>> The reason why I raised this question is that languages like C++ do
>> allow hashes arrays
>
> Please don't call hashes "hashes arrays". They are either "hashes" or
> "associative arrays". (Technically, hashes are the name of the implementa=
tion
> rather than the Abstract Data Type of a Dictionary that matches keys to v=
alues
> , but we call that in Perl because it's a short and convenient word).
>
>> that can have different values but same key. Thats the reason for the
>> second part of my
>> question.
>
> Are you talking about the C Standard Template Library (STL)? How does wha=
t you
> are referring to allow multiple values for the same key? I should note th=
at if
> you wish to do something like that in Perl, you can have the key point to=
a
> value that is an array reference, a hash reference or a different data
> structure.
>
> Regards,
>
> =A0 =A0 =A0 =A0Shlomi Fish
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish =A0 =A0 =A0 http://www.shlomifish.org/
> Stop Using MSIE - http://www.shlomifish.org/no-ie/
>
> God considered inflicting XSLT as the tenth plague of Egypt, but then
> decided against it because he thought it would be too evil.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply =
..
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
On 2 August 2010 12:11, Sharan Basappa <sharan.basappa [at] gmail.com> wrote:
> I am not trying to be critical of perl or anything (if that is what you felt).
I didn't think you were; it didn't come across as criticism.
> I am only trying to see if a certain feature exists or not.
> The current problem I am working on has duplicate key values and hence
> the question.
>
> The STL multipmap library explanation is below:
> http://www.cplusplus.com/reference/stl/multimap/
An equivalent perl construct to an STL multimap is a hash of array references:
my %hash = (
keyA => [valueA valueC valueF],
keyB => [valueB valueH],
);
For much more detail, please see the following perldoc pages: perldata
perllol perldsc perlref.
Phil
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
Hi Sharan,
On Monday 02 August 2010 14:11:32 Sharan Basappa wrote:
> Shlomi,
>
> I am not trying to be critical of perl or anything (if that is what you
> felt).
That's not how I felt.
> I am only trying to see if a certain feature exists or not.
> The current problem I am working on has duplicate key values and hence
> the question.
Then please explain what you are trying to achieve, and ask for good solutions
to the problem. Don't beat around the bush asking if Perl has the feature
"frobnix" or something else you're familiar with.
>
> The STL multipmap library explanation is below:
> http://www.cplusplus.com/reference/stl/multimap/
>
OK, I'll try to take a look.
Regards,
Shlomi Fish
> Regards,
> Sharan
>
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
On Aug 2, 3:13=A0am, sharan.basa... [at] gmail.com (Sharan Basappa) wrote:
> On Mon, Aug 2, 2010 at 3:36 PM, Uri Guttman <u... [at] stemsystems.com> wrote:
> >>>>>> "SB" =3D=3D Sharan Basappa <sharan.basa... [at] gmail.com> writes:
>
UG> > =A0SB> Is there any restriction that the keys in perl should be
unique?
UG> > =A0SB> If not, are there any special considerations that need to
be kept
UG> > =A0SB> 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'} =3D 'Larry' ;
$h{'Wall'} =3D 'Brick' ; # Note the duplicate key
$h{'Wall'} =3D 'Brick' ; # Note the duplicate key and value
...
for ( ... } { print "$key -> $value\n" }
---> Wall -> Brick
---> Wall -> Brick
----> Wall -> Larry
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: has and non unique keys
>>>>> "CD" == C DeRykus <derykus [at] gmail.com> writes:
CD> DB_FILE's module implements a 'hash-like' type BTREE
CD> based on the Berkeley DB API which can store/retrieve
CD> duplicate keys.
CD> $h{'Wall'} = 'Larry' ;
CD> $h{'Wall'} = 'Brick' ; # Note the duplicate key
CD> $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
that is the heart of most ISAM (indexed sequential access method)
implementations. you can locate a key and then sequentially read records
from that point in sorted order. so duplicate keys are fine there. but
isam is not a common language feature. PL/I and cobol both supported
isam among other recond i/o systems. i don't know of any modern language
that directly supports isam (vs loading it via a module). most have
hashes or similar constructs as it is easy to implement, fast and they
have clean semantics. isam is wierd from a language view unless you
really need both direct and sorted sequential access. then use a module.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/