Python/C++ vs. Perl/C++

Printing a Hash Member

Here's a little exercise that should demonstrate that Python is a more natural scripting language option for C/C++ programmers (and human beings in general) than Perl. This is but one of many possible examples.

The task: Print the contents of a list that is a member of a hash (Perl) or dictionary (Python). We'll skip any C++ here as this is more of a scripting language thing.

Comment on this topic

Perl
my @list = ('a', 'b', 'c');
my %hash;
$hash{'List'} = \@list;
print "@{$hash{'List'}}\n";

Note the difficulty in just constructing the nested data structure. You have to explicitly add a reference to the list as the hash element. As for the printing of the list, since we are storing a reference to the list in the hash, we must coerce that reference into its actual type by prepending with @, which just adds to the ugliness required to simply print the value stored in the hash. There are seven characters in those four lines of code just to handle all the typing and coercing and referencing required by Perl (I'm being nice and not counting the use of "my" which also clutters up the code).

Python
list = ['a', 'b', 'c']
hash = {}
hash['List'] = list
print hash['List']

Clean. Easy.