Rekeying a table - comparison in #Ruby #Perl and #Python
Archive - Originally posted on "The Horse's Mouth" - 2011-02-14 18:31:31 - Graham EllisAlbert knows Perl and Python, Barbara knows Lua and Ruby, Colin knows Perl, and Debbie is a dab hand at PHP. Eddie does Python, Perl and PHP and Fiona cooks a great dinner for Shrek. Gordon programs in Lua and PHP, and Harriet knows both Ruby and Perl. Who shall I ask for help on a Perl project?
The requirement to take a who series of records keyed by one field and rekey it by another is not an uncommon one, and it's something that Perl does very well - in fact you could say that it's an ideal application for Perl. Using a hash, you can read through each line of the data - each person's record - and build up a hash of lists which you can report from at the end, when you have read in all your records. The use of a hash, in which Perl can locate records by key very quickly, makes this operation efficient even for quite large data sets.
But Perl isn't in fashion as it was 10 to 15 years ago; people dislike some of the syntax such as the lack of a conventional OO interface and style, and they dislike the complexity of some of the statements (though they may delebrate their shortness_ - too many cases of % and @ and { and [ coming in a great long series. People worry about Perl's assumption that the programmer knows what he's doing (which can lead to some extraordinary results when it turns out (s)he didn't, and they're concerned at just how long they's been waiting for the first production version of Perl 6 - "we'll get it right, not rush it" the drivers of Perl 6 tell people; yes - that's good, but people can get a bit impatient after 10 years.
So other languages, which solve these issues, have become much more popular than they would have done if Perl 6 had been available in 2006. Ruby has been described as a sort of "Perl 5 and a half", and we see it moving from a single-application language (always used as part of 'Ruby on Rails') to a more general language. And Python, though it's been around a long time has very much come into its own - indeed Python, it transpires, was very much a language ahead of its time and is continuing to grow in popularity as Perl seems to be just gently plodding along in a quieter backwater.
The week before last, I wrote a Ruby program to invert a file of data as described at the top of this article, and I promised my delegates (I was giving a course at the time) that I would publish it here. I'm now writing this article seated in an train on my way to Germany to give a Python course, and I have taken the opportunity to recode the same application - for comparison - into Perl and Python.
"How about writing it in PHP too?" asks Lisa, seated beside me. No - I'm not going to encourage you to use PHP if this is the sort of thing you'll be doing a lot of. PHP's associative arrays use a different storage technique which have the advantage over Perl and Ruby hashes / Python dictionaries that they can be sorted, but the disadvantage that they get rather slower once you pile huge amounts of data in. In any case, there's already an example of analyzing teh same file for a particular language in PHP [here] which shows that - if you need to invert a little bit of data - you can do it easily enough.
Example sources ... [Ruby]i ... [Perl] ... [Python] ... and an example of how they differ:
Perl:
push @rz,$name if ($lang eq "PHP");
Python:
if lang == "PHP": rz.append(name)
Ruby:
rz.push(name) if lang == "PHP"
Course schedule - for Perl, Python, Ruby (and PHP and other languages too!) [here]