Perl provides for free many of the data structures that
you have to build yourself in other programming languages. The stacks
and queues that budding computer scientists learn about are both just
arrays in Perl. When you
unshift) an array, it's a stack; when you
pop) an array, it's a
queue. And many of the tree structures in the world are built only to
provide fast, dynamic access to a conceptually flat lookup table.
Hashes, of course, are built into Perl, and provide fast, dynamic access
to a conceptually flat lookup table, only without the mind-numbingly
recursive data structures that are claimed to be beautiful by people
whose minds have been suitably numbed already.
But sometimes you want nested data structures because they most naturally model the problem you're trying to solve. So Perl lets you combine and nest arrays and hashes to create arbitrarily complex data structures. Properly applied, they can be used to create linked lists, binary trees, heaps, B-trees, sets, graphs, and anything else you can devise. See Mastering Algorithms with Perl (O'Reilly, 1999), the Perl Cookbook (O'Reilly, 1998), or CPAN, the central repository for all such modules. But simple combinations of arrays and hashes may be all you ever need, so they're what we'll talk about in this chapter.
There are many kinds of nested data structures. The simplest kind to build is an array of arrays, ...