Skip navigation.
Your source for Perl tips, howto's, faq and tutorials

hash of hashes

( categories: )

A hash of hashes is a two-dimensional structure that consists of a hash whose elements are references to other hashes.

- Static initialization

We use anonymous hashes to define the subhashes that are the elements of the hash:


%hash_of_hasess =  ( 'first' =>  {
                                    'one'   => '1', 
                                    'two'   => '2', 
                                    'three' => '3',
                     'second' => {
                                    'four'  => '4',

array of arrays

( categories: )

An array of arrays is a two-dimensional structure that consists in an array whose elements are references to other arrays.

- Static initialization

Each element of the array (let's call them subarrays) is also an array, so we use anonymous arrays to define each subarray:


@array_of_arrays =  ( [ "one", "two", "three" ],
                      [  4,   5,  6,  7  ],
                      [ "alpha", "beta" ]

To access the last element of the second array (number 6 in the example):

introduction to references

( categories: )

Understanding references is vital to work with complex data structures; that's the reason for this short introduction.

A reference is basically a pointer to another object. It's as simple as that.

A very important fact is that these pointers are stored in scalar variables. As hashes and arrays are basically collections of scalar variables, you can have array or hash elements that are in fact references to other structures; this way you can easily construct structures like array of arrays, array of hashes, etc.


There are two types of references:

Syndicate content