Mailing List Archive

Odd numbers of elements in hash (Perl bug NETaa14645). Was: Re: Diff two arrays.
[Cced to perlbug]

In article <46902i$98u@gateway.grumman.com>,
John Allen <allen@gateway.grumman.com> wrote:
>In article <ukybug7mjn.fsf@linda.teleport.com>,
>Randal L. Schwartz <merlyn@stonehenge.com> wrote:
>>>>>>> "Todd" == Todd Beverly <tb@vegas.kodak.com> writes:

>>Todd> %mark = [];

>>You have just created an associative array with one element whose key
>>is the stringificiation of the reference to an anonymous list, and value
>>is undef.

>I think he just created an empty associative array.
> perl -e '%a = []; print keys %a'

> prints nothing

>Isn't this because valueless keys are not added to a hash?

No, it's a bug in Perl, NETaa14645, unresolved as at 7 September (which
is when Tom stopped updating the bugs database on www.perl.com).

http://mox.perl.com/perl/bugs/NETaa14645-1.html

contains Gurusamy Sarathy's report, and includes a patch.

I note that it (the patch) reports both "Odd number of elements in hash
list" and "Attempt to use reference as hash key" using warn(), although
they are documented as (S) and (W). Some reconsistencification needed
here, methinks. IMO, those levels are right, although it would be nice
to be able to use refs as hash keys one day.

Ian
Re: Odd numbers of elements in hash (Perl bug NETaa14645). Was: Re: Diff two arrays. [ In reply to ]
On Sun, 22 Oct 1995 11:25:34 GMT, ian wrote:
>[Cced to perlbug]
>
>>I think he just created an empty associative array.
>> perl -e '%a = []; print keys %a'
>
>> prints nothing
>
>>Isn't this because valueless keys are not added to a hash?
>
>No, it's a bug in Perl, NETaa14645, unresolved as at 7 September (which
>is when Tom stopped updating the bugs database on www.perl.com).
>
>http://mox.perl.com/perl/bugs/NETaa14645-1.html
>
>contains Gurusamy Sarathy's report, and includes a patch.
>
>I note that it (the patch) reports both "Odd number of elements in hash
>list" and "Attempt to use reference as hash key" using warn(), although
>they are documented as (S) and (W). Some reconsistencification needed
>here, methinks. IMO, those levels are right, although it would be nice
>to be able to use refs as hash keys one day.

I believe this was discussed at length a few months ago when the
inefficiency of storing duplicate literal hash keys was raised. Allowing
references as hash keys would be one way to eliminate this problem, (but
I think duplicate literal elimination really belongs inside perl code).

Since references as hash keys is more often than not an error, we might
think about an explicit pragma:

use hash refs;
no hash refs;

From a cursory examination of the source this looks rather easy to
achieve. But I may be mistaken.

>
>Ian
>

- Sarathy.
gsar@engin.umich.edu
Re: Odd numbers of elements in hash (Perl bug NETaa14645). Was: Re: Diff two arrays. [ In reply to ]
On Sun, 22 Oct 1995, Gurusamy Sarathy wrote:

> I believe this was discussed at length a few months ago when the
> inefficiency of storing duplicate literal hash keys was raised. Allowing
> references as hash keys would be one way to eliminate this problem, (but
> I think duplicate literal elimination really belongs inside perl code).
>
> Since references as hash keys is more often than not an error, we might
> think about an explicit pragma:
>
> use hash refs;
> no hash refs;
>
> >From a cursory examination of the source this looks rather easy to
> achieve. But I may be mistaken.

Don't forget the more subtle problem with using refs as keys: how is the
actual hash function calculated? If it's based on the ref itself, then
two different refs to the same object can be entered into the hash, and
generating a ref of an object might not be sufficient to fish it out of
the hash.

> >
> >Ian
> >
>
> - Sarathy.
> gsar@engin.umich.edu
>

--
Kenneth Albanowski (kjahds@kjahds.com, CIS: 70705,126)