Perl Array

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


Arrays are lists of scalars. Perl Array names begin with @. Perl Arrays are defined by listing the content in parentheses, separated by commas:

@newarray = (1, 2, 3, 4, 5, 6);
@montharray = ("Jan", "Feb", "March");

The contents of an array starts from index 0. To fetch an elements of the array, you use a $ sign followed by the index position of the element as follows:

    @montharray = ("Jan", "Feb", "March");
    print $montharray[0];   # This prints "Jan".
    $montharray[2] = "August";  # We just renamed March to August.

implicitly creates @winter_months.

The length of the array can be find out by using the value $#array_name. This is one less than the number of elements in the array.

     @montharray = ("Jan", "Feb", "March");
    print $#montharray;         # This prints 3.
    $#montharray = 0;           # Now @montharray only contains "Jan".

In the 3rd line above, we resized the array size to 1.

You can refer this link for more detail: Perl Arrays





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 <% ... %>.