[Israel.pm] Python talk

Gaal Yahas gaal at forum2.org
Tue Dec 7 12:38:45 PST 2004


On Tue, Dec 07, 2004 at 08:25:31PM +0000, Mikhael Goikhman wrote:
> > > >     $time2 = [ 100, 200 ];
> > > > 	return $CAA->{$time2}; #undef
> > > > 
> > > > What I typically want for object keys is that they associate to the same
> > > > element if they pass (in Javaesque parlance) an isEqual test.
> > > 
> > > It seems you completely missed my note that this limited technique only
> > > works "if the keys are constant arrays and not variable arrays". :)
> > 
> > Indeed I did not!
> > 
> > @constant_array = ( 100, 200 );
> > $key1 = \@constant_array;
> > $key2 = \@constant_array;
> 
> I used "constant" in the "use constant" context. Here is how it works.
> 
>     use constant {                                         
>         INTERVAL => [ 3.1415, 92653 ],  
>         PRODUCT1 => { name => "ice", price => 1E6 },
>     };
>     $hash{&INTERVAL} = 58979;
>     $hash{&PRODUCT1} = "constant arrayref and hashref as hash keys";
>     print "$hash{&PRODUCT1}, $hash{&INTERVAL}\n", keys %hash, "\n";

Yes, I understand, but as you know the data in these cases isn't what's
constant. The reference to the data is. (In Perl, the type of data a
reference holds can't be changed, too, at least not easily; and if it
*is* somehow changed by mucking about in XS, the stringification of
the reference will change.) As I said, what I *typically* want is not
identity, but equality. YMMV, of course.

If the key is somehow computed by input from the user ("give me a
coordinate" -- "hmm, let's see, you live in a city of population 17"),
then you have to do some funky work to find the reference that holds
equivalent data, just so you can do the lookup. But if you do that, why
don't you store that extra data on the reference in the first place?

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list