Perl Array of Arrays: Tutorial

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


Perl Array of Arrays:

Array of arrays allow us to use arrays within an array. This is a two-dimensional data structure that is quite often used in the perl environment.

Perl array can have multiple arrays referenced within it.

Perl Array of Arrays Example:

@perl_array_of_arrays =  ( [ "perl_array_element_1", "perl_array_element_2", "perl_array_element_3" ],
                      [  4, 5,  6,  7  ],
                      [ "perl_array_element_alpha", "perl_array_element_beta" ]
                    );

To access second element of the second perl array of arrays, we would use:

$perl_element = $array_of_arrays[1][1];

$perl_element would be 5. As usual with perl arrays, first array is accessible at index 0, second array at 1 and third array at 2.

Foreach with Perl Array of Arrays:

Following is the way, we would loop in the perl array of arrays:

foreach $row (0..@perl_array_of_arrays)
{
  foreach $column (0..@{$perl_array_of_arrays[$row]})
  {
    print "Element [$row][$column] = $perl_array_of_arrays[$row][$column];
  }
}





Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.