August 15, 2003

The One True Reason to use Python over Perl

I've read many discussions about the relative merits of either Python or Perl. The honest truth for most people is that it is more efficient in the short-term just to use what you know than to learn a whole new system. However, I believe strongly that in the long haul, you'll be happier using Python than Perl. I don't want to have a debate about it, but I do want to share the one true reason. Lets convert the following Python fragment to Perl:
#!/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 5:02 PM