On Tue, Jul 23, 2002 at 03:10:50PM +0000, Ashley Winters wrote:
> On Tuesday 23 July 2002 08:44 am, Alberto Manuel Brandão Simões wrote:
> > On Tue, 2002-07-23 at 09:27, Ashley Winters wrote:
> > > @foo = ();
> > > %hash{@foo} = 10;
> > > push @foo, 'This would change the hash key for @foo?';
> > >
> > > print "ok 1" if exists %hash{ [] };
> > > print "ok 2" if exists %hash{ ['This would change the hash key for
> > > @foo?'] }; print "ok 3" if exists %hash{@foo};
> > >
> > > What's going to get printed from that?
> >
> > IMHO, it should print 'ok 1'. The idea of the hash function is to use
> > PMC's as hash keys. That means that different content PMC (as an array
> > with different elements) must return different hash keys.
> 
> I can see the argument both ways, but I have a proposal.
> 
> @foo = (1 .. 100);
> %hash{@foo} = "reference";
> %hash{@foo.freeze} = "contents";
> 
> %hash{@foo} eq "reference";
> %hash{[1..100].freeze} eq "contents";
> 
> I'm just using "freeze" as the serialization routine until a real name is 
> determined. If I need a "unique" key for hashing based on aggregate content, 
> can I ass_u_me the builtin Perl6/Python/etc serialization mechanism will 
> suffice?

I can't see any other way. If you want to hash by value, then you need
to use a string which is the serialisation (eg using Data::Dumper, Storable
or their ilk in perl5). A second indentical structure would be C<eq> even
if it's in a different PMC. Anything less and you can't be sure whether two
independently constructed structures are deeply C<eq>

If you want to hash by reference, then you need a string that reflects
something invariant about that PMC. In perl5 terms this is stringifying a 
reference, such as SCALAR(0x1118f0) - it's unique to that scalar. ($^X in
the case above), but I'm not sure whether addresses in parrot will move about
due to GC. Something would only be C<eq> if it happens to refer to that same
PMC, but would remain C<eq> even if the value of that PMC has been updated
since first hashed.

Nicholas Clark
-- 
Even better than the real thing:        http://nms-cgi.sourceforge.net/

Reply via email to