3.1.3 Elements of an Array or a List
We can access an individual element of an array by using its index. Elements are indexed in Perl starting with the number 0. Therefore, if we have an array @friends, its first element is actually its zeroth element. To access the zeroth element of @friends, we write $friends [0]. The @ sign in front of the name of the array changes to a $ to signal that we are focusing on one element of the array and each element of an array is a scalar. The index 0 is included in square brackets.
If @friends has 20 elements in it, the index of the last element in it is 19. The last element is obtained by writing $friends [19].
An element of an array is a scalar, and hence, assigning a value to an element of an array is the usual scalar assignment operation. We individuate the element of the array using an index and place it to the left of the assignment operator. For example, to assign a value to the $ith element of the array @friends, we write the following.
$i = 6;
$friends [$i] = "Chad Blonding";
Here, $i has been given the value 6 prior to the assignment. It is not an error to have an array literal when when we obtain an element of an array using an index. Therefore the following is correct.
$friend = ("John", "Jeff", "Brett", "Justin", "Tom") [2];
