Arrays are a special type of variable that allows a list of data to be stored in Perl.
@myArr = ('A', 'B', 'C');
You have to use the
@ symbol before the variable’s name to declare an array. The list of data items, also known as elements, is written inside the round brackets separated by commas.
Accessing an element in an array is known as indexing. In Perl, we do it as follows:
@myArr = ('A', 'B', 'C'); print "Second element of myArr is $myArr\n";
In the code above, we first declare an array
@myArr, and then print its second element. To print the second element, we place the
＄ sign before the variable name
myArr, and then specify the index
 in the square brackets.
Array slicing helps extract multiple elements from the array. This is done using the two methods discussed below.
One of the ways to slice an array is to use multiple indexes while indexing the array. See the example below.
@myArr = ('I', 'love', 'this', 'shot'); @slicedArr = @myArr[1, 2, 3]; print "Sliced elements: @slicedArr";
In the example above, the array
myArr contains four string elements. We extract the last three elements and store them inside the
@slicedArr variable. To slice the
myArr, we provide the indexes of the elements we need inside the square brackets separated by commas.
Another method to slice the array is by using a range operator. A range operator consists of 2 consecutive dots
... On the left side of this operator, we provide the starting index and on the right side, we provide the ending index. The following code provides an example.
@myArr = ('I', 'enjoyed', 'reading', 'this', 'shot', '!'); # Starting index (2) is provided before the range operator # Ending index (4) is provided after the range operator @slicedArr = @myArr[2..4]; print "Sliced elements: @slicedArr";
In the example above, the array
myArr contains six string elements. We extract the 3rd, 4th, and 5th elements and store them inside the
The following code snippet demonstrates the behavior of the range operator when the provided indices are invalid.
@myArr = ('I', 'enjoyed', 'reading', 'this', 'shot', '!'); @slicedArr1 = @myArr[-1..4]; @slicedArr2 = @myArr[2..6]; @slicedArr3 = @myArr[6..6]; print "Sliced elements: @slicedArr1\n"; print "Sliced elements: @slicedArr2\n"; print "Sliced elements: @slicedArr3\n";
In the code above, the range operator is used on
myArr with different indexes, and the results are stored in
slicedArr3. The first index specified for
-1, apparently an invalid index (since indexing begins from
However, negative indexes are treated as indexes counting from the end of the array. For example, the element on index
!, the element on index
shot, and so on up to the first element. Therefore, the
slicedArr1 stores the elements
('!', 'I', 'enjoyed', 'reading', this, 'shot').
slicedArr2, the second index specified is
6, which is invalid since the last element index is
5. Since the second index of the range operator is used to specify the ending index of the slice, if that index is greater than the last index of the array, then the effect is as if the last index was used in the range operator.
slicedArr3, both the first and the second indexes are
6 (greater than the last index of
myArr). Since the first index of the range operator is greater than the last index of
5), no slicing takes place, and an empty slice is returned.
View all Courses