Ïðèãëàøàåì ïîñåòèòü
Ìàðêåòïëåéñ (market.find-info.ru)

Recipes for Structures

Previous Table of Contents Next

Recipes for Structures

The following sections present some of the more common arrangements of lists and hashes into structures.

Example: A List of Lists

Lists of lists in Perl are often used to represent a common structure called a two-dimensional array. That is, a normal array is a linear list of values, as illustrated here:

Recipes for Structures

A two-dimensional array is like a grid of values in which each element is addressed like a point with coordinates along two axes. The first part of the index indicates a row number (starting with 0), and the second is the column, as you can see here:

Recipes for Structures

Perl doesn't really support a true two-dimensional array. However, Perl does allow you to emulate the two-dimensional array using an array of array references.

To create an array of arrays, use this literal representation:


@list_of_lists=(

    [ qw( Mustang Bronco Ranger ) ],

    [ qw( Cavalier Suburban Buick ) ],

    [ qw( LeBaron Ram ) ],

);


Look carefully at the preceding snippet. A regular list—@list_of_lists—is being created, but it consists of references to other lists. To access individual elements of the innermost lists—cells in the two-dimensional array—you can use the following code:


$list_of_lists[0][1];    # Bronco.  1st row, 2nd entry

$list_of_lists[1][2];    # Buick.  2nd row, 3rd entry


To find the number of elements in the outer list, do so as you would for any other array—use the $# notation or use the array name in a scalar context:


$#list_of_lists;         # Last element of @list_of_lists: 2

scalar(@list_of_lists);  # Number of rows in @list_of_lists: 3


Finding the number of elements in one of the inner lists is a little trickier. The syntax $list_of_lists[1] returns the reference in the second row of @list_of_lists. Printing it displays something like ARRAY(0x00000). To treat an element of @list_of_lists as if it were an array, put an @ sign in front of it, like this:


scalar(@{$list_of_lists[2]});   # Number of elements in the 3rd row

$#{$list_of_lists[1]};          # Index of last element in the 2nd row


To traverseevery element in the list of lists, you can use this code:


foreach my $outer (@list_of_lists) {

    foreach my $inner (@{$outer}) {

        print "$inner ";

    }

    print "\n";

}


You can add to the structure like this:


push(@list_of_lists, [ qw( Mercedes BMW Lexus ) ]);  # A new row

push(@{$list_of_lists[0]}, qw( Taurus ) );           # A new element to one list


Other Structures

In the preceding section, you learned how to create a basic Perl structure, a lists of lists, by using references and arrays. Actually, an unlimited number of variations of arrays, scalars, and hashes can be combined to create more and more complex data structures, such as the following:

  • Lists of hashes

  • Hashes of lists

  • Hashes of hashes

  • Hashes that contain lists, which contain hashes, and so on

There isn't enough room in this book to describe all of these structures. However, the online documentation that comes with each Perl installation contains a document titled the Perl Data Structures Cookbook. It is a very detailed but understandable description of each of these structures and many others. With each structure, the Perl Data Structures Cookbook details

  • Declaring your structure (literal representation)

  • Filling your structure

  • Adding elements

  • Accessing elements

  • Traversing the entire structure

To view the "Perl Data Structures Cookbook," at a command prompt, type perldoc perldsc.

Debugging with References

When debugging programs with references, it's not uncommon for new programmers to get confused about which references point to which kinds of structures. Also, the syntax can be confusing until you get used to it. For that reason, Perl provides some facilities to help you figure out what's going on.

First, you can simply print the reference. Perl displays what the reference points to. For example, the line


print $mystery_reference;


might display


ARRAY(0x1231920)


This result means that the variable $mystery_reference is a reference to an array. Other possibilities include references to scalars (SCALAR), hashes (HASH), or subroutines (CODE). To print the array referred to by $mystery_reference, you can treat it like an array, as shown he
Previous Table of Contents Next