Perl updating hash of arrays

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. And many of the tree structures in the world are built only to provide fast, dynamic access to a conceptually flat lookup table.


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.

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.


(The obvious generalization applies: an array of arrays of arrays is a three-dimensional array, and so on for higher dimensions.) It's reasonably easy to understand, and nearly everything that applies here will also be applicable to the fancier data structures that we'll explore in subsequent sections.# Create an reference to an array of array references.

$ref_to_Ao A = [ [ "fred", "barney", "pebbles", "bamm bamm", "dino", ], [ "homer", "bart", "marge", "maggie", ], [ "george", "jane", "elroy", "judy", ], ]; print $ref_to_Ao A- Those big list assignments are well and good for creating a fixed data structure, but what if you want to calculate each element on the fly, or otherwise build the structure piecemeal? We'll assume that it's a plain text file, where each line is a row of the structure, and each line consists of elements delimited by whitespace. The following code prints the whole structure, looping through the elements of If you want to access a slice (part of a row) of a multidimensional array, you're going to have to do some fancy subscripting.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>