#!/usr/bin/python
a = { 'b' : { 'c' : 1 } }
for i in a.values():
for k in i.values():
print k
It is simply a nested loop iteration over a hashtable of hashtables of ints. However, in Perl, this requires a confusing spurious %{ $i }. That's alot of punctuation. This punction is basically telling perl that even though $i needs to be a scalar inside the context of the foreach loop, it should force it to be a hash for the purpose of the input to the next foreach. I challenge you to show us all another way. Here is the code:
#/usr/bin/perl
$a{"k"}{"r"} = 1;
foreach $i (values %a) {
foreach $j (values %{ $i }) {
print "$j\n";
}
}
Posted by jeske at August 15, 2003 5:02 PM
print $a{"k"}{"r"}, "\n";
How do you say this in Python?
As a Perl nut, the Python statement:
a = { 'b' : { 'c' : 1 } }
Blows my mind far worse than understanding that you have to explicitly explain that a thingy extracted from a hash should be handled as a hash. This is something you have to do sometimes in a weakly-typed language.
I like Perl because the old Perl 4 O'Reilley book was fun to read through, explaining how Noah would develop an increasingly more sophisticated program to keep tabs on his animal inventory, printing formatted reports to /dev/ctp (clay tablet printer).
Whereas the Python documentation tends to read like a boring CS text saying "look how elegant we are in the abstract - data structures that are beautiful, if data structures get you off." As one who majored in CS and majored in English, I find the "Noah using a programming language to manage his animals" a far more heartening introduction.
Posted by: Danny Howard at August 18, 2003 12:21 PMYou're right that the loops require more punctuation, but you're a little overpunctuated yourself there. This is perfectly idiomatic:
$a{b}{c} = 1;
foreach $i (values %a) {
foreach $j (values %$i) {
print "$j\n";
}
}
(Unfortunately, your feedback form strips the indentation... which would break any Python examples, I might add!)
Perl stringifies identifiers inside hash lookups, and has reasonable syntax for simple dereferences. If syntactic regularity is what you want, use a hash reference instead of an actual hash:
foreach $i (values %$a) {
foreach $j (values %$i) {
print "$j\n";
}
}
In the initialization lines, you're comparing apples to oranges. In the Perl example, you're setting an explicit value of one hash; in
the Python example, you're setting the entire hash. Other than the fact that in Perl variables look like $this, if you do the same thing in both
languages the result is virtually identical:
a = { 'b' : { 'c' : 1 } }
$a = { b => { c => 1 } };
I don't see any legibility difference here.
It sounds like your argument for Python for Perl boils down to this:
Perl has more punctuation than Python.
Yes. You're right about this. Like Ruby, Python was designed with elimination of punctuation in mind. But this is a somewhat poor example
to use for comparison, and it's unclear to me that reduced puncutation is the one factor that correlates programming languages with happiness,
to the exclusion of all others.