Chapter 9. Data Structures
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 push and pop (or unshift and shift) an array, it’s a stack; when you
push and shift (or unshift and 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 they 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, Perl Cookbook, the “Data Structure Cookbook” in perldsc, 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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access