[Israel.pm] Python talk

Mikhael Goikhman migo at homemail.com
Tue Dec 7 01:22:01 PST 2004


On 07 Dec 2004 10:48:29 +0200, Uri Bruck wrote:
> 
> Mikhael Goikhman wrote:
> > On 07 Dec 2004 05:46:27 +0200, Omer Zak wrote:
> >
> > > I'll write, in Python, an associative array, whose keys are arrays
> > > and its data are themselves associative arrays.  Show me how to
> > > write it in Perl.
> >
> > Are you sure the keys of your COMPLICATED_ASSOC_ARRAY may be associative
> > arrays by themselves? Python gives me an arror when I try to do this.
> > It seems that the keys of the Python "hash" can be "arrays", but not
> > "hashes". So, your statement above is not true.

First, I am sorry for this last paragraph, I didn't read it correctly.
Omer didn't claim Python supports keys that are hashes themselves.

> > > COMPLICATED_ASSOC_ARRAY = {
> > >   (0,0) : { "label" : "Origin of the World", "population" : 0 },
> > >   (0,90): { "label" : "North Pole", "population" : "varies, usually 0"},
> > >   (0,-90):{ "label" : "South Pole", "population" : ("penguins",10E6)}
> > > }
> >
> > Ok, Perl only allows strings or numbers to be the hash keys.
> 
> perl allows hash keys such as
> $COMPLICATED_ASSOC_ARRAY{0,90}
> $COMPLICATED_ASSOC_ARRAY{'foo',1}
> etc.
> The documentation for the module Offer found even compares this to what 
> the module offers.

Yes, but with this syntax, Perl still converts the list to string, much
like the code below, it just uses a separator different from the comma.
So, my statement that internally Perl only allows strings or numbers for
the hash keys is correct. I didn't want to suggest this list syntax,
because it does not work when defining the complex structure inline,
something that Omer wanted. So, it seems better to always stick to the
string keys, like in the code below.

> > to simulate keys that are arrays, you may map array to string. So
> > here is the Perl code:
> > 
> > $COMPLICATED_ASSOC_ARRAY = {
> >   '0,0'   => { label => 'Origin of the World', population => 0 },
> >   '0,90'  => { label => 'North Pole", population => 'varies, usually 0' },
> >   '0,-90' => { label => 'South Pole", population => [ penguins => 10E6 ] },
> > }

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list