Chapter 11. References
Mark Jason Dominus
One of the most important new features in Perl 5 was the capability to manage complicated data structures like multidimensional arrays and nested hashes. To enable these, Perl 5 introduced a feature called references, and using references is the key to managing complicated, structured data in Perl. Unfortunately, there’s a lot of funny syntax to learn, and the manual is not as clear in this area as it usually is. The manual is quite complete, and a lot of people find that a problem, because it can be hard to tell what is important and what isn’t.
Fortunately, you only need to know 10% of what’s in the manual to get 90% of the benefit. This article is going to show you that 10%.
Who Needs Complicated Data Structures?
One problem that came up all the time in Perl 4 was how to represent a hash with values that were lists. Perl 4 had hashes, of course, but the values had to be scalars; they couldn’t be lists.
Why would you want a hash of lists? As a simple example, suppose you have a file of city and state names, like this:
Chicago, Illinois New York, New York Albany, New York Springfield, Illinois Trenton, New Jersey Evanston, Illinois
You want to produce an output like this, with each state mentioned once, and then an alphabetical list of the cities in that state:
Illinois: Chicago, Evanston, Springfield. New Jersey: Trenton New York: Albany, New York.
The natural way to do this is using a hash with keys that are state names. Associated with each ...