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 EllisIn 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.

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.