[Previous] [Contents] [Next]


Data Structures

Nested data types and other data structures are now possible, using references. You'll also learn about this topic later when we look at references in depth. A simple example that implements an array of arrays follows:

@foo = ( 1, 2, 3);

 @bar = ( 4, 5, 6);

 @arrayref = (\@foo, \@bar);

 print "The third element of \@foo is $arrayref[0][2]\n";



 # prints: The third element of @foo is 3


Here, I specify the zero'th element of @arrayref, which is an array of references to arrays, and then use it to dereference the third element of @foo. @arrayref actually looks and feels just like a two-dimensional array, but it's important to understand that it isn't.

Perl does not support multidimensional arrays. All arrays are still flat. By using references, however, you can emulate multidimensional arrays and build up complex data structures. See PERLDSC for a compendium on Perl data structures.

[Previous] [Contents] [Next]