[Tutor] translating some python code to perl

Lloyd Kvam pythontutor at venix.com
Wed Aug 27 18:31:50 EDT 2003


I need to provide the contents of a python dictionary to a perl
programmer as a perl hash table.  However, the python dictionary
contains dictionaries, and I have been unable to figure out the perl
syntax for nexting and referencing hash tables.

Here's a python sample:
#python
d1 = {'1': 'one',
     '2': 'two',
     'd2': {'nest1': 'nest one',
         'nest2': 'nest two',}
}
d1['1'] # returns 'one'
d1['d2'] # returns the d2 hash
d1['d2']['nest1'] # returns 'nest one'

Here is my attempted perl equivalent:
#!/usr/bin/perl
# testhash.pl

%d1 = (
     '1' => 'one',
     '2' => 'two',
     %d2 = ('nest1' => 'nest one',
     	'nest2' => 'nest two',)
     );

print "\nlookup 1 in d1: $d1{'1'}\n";
print "\nlookup d2 within d1: %d1{'d2'}\n";
print "\nlookup nest1 in d2 within d1: $d1{d2}{'nest1'}\n";
###end of perl script

$d1{'1'} returns the scalar 'one'

So that's as far as I was able to go.  I am not sure if my hash table
is correct.

Please help me fix the hash table definition and the references so that
I can verify that it works.

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582




More information about the Tutor mailing list