Main Content

Transforming data in Perl using lists of lists and hashes of hashes

Archive - Originally posted on "The Horse's Mouth" - 2009-06-12 16:25:00 - Graham Ellis

In Perl, you don't have conventional two dimensional arrays - you have lists which are single dimensional only. However, you can have a list that contains a reference to another list, in effect a list of lists. Very powerful stuff as it leads to a very flexible way of handling data structures - we cover it briefly on our Perl Programming Course and in much more depth on Perl for Larger Projects.

As you write code using lists of lists (and hashes of lists and the other alternatives too), you are very strongly advised to draw a diagram of what you are trying to achieve. It will do wonders for you. That way you can see the difference between a list which is a concatenation of other lists, and a list of lists. Say, for example, I had three options for breakfast, 4 for lunch and 4 for dinner, a concatanated list would be 11 elements long, but a list of lists would contain three elements. There's a source code example here from today's course. The diagram alongside shows how this code works.


One of the big uses of hashes of lists (as an example of a collection of collections) is in inverting data. Let's say I have a file in which each line starts with a person's name, and the person's skills are then listed along the line. But I want to turn that around so that I can see a list of skills, with the people holding each skill alongside the skill. It turns out to be a handful of lines of code - see here. What works for people and skills is equally good in transforming a web access log file, listing by visiting client, into a report on each of the web pages on your website and telling you how many people have visited each of them!

Three further examples: reading the people / skills file into a list of hashes and the data inversion with a web (CGI) interface. Finally, the data inversion with a search facility.