Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl
communitycreator

What are indexes and slices in an array in Perl 5.3.4?

Hammad Nasir

Arrays are a special type of variable that allows a list of data to be stored in Perl.

Syntax

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

Array indexing

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[1]\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 [1] in the square brackets.

Array slicing

Array slicing helps extract multiple elements from the array. This is done using the two methods discussed below.

Multiple index values

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.

Range operator

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 @slicedArr variable.

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 slicedArr1, slicedArr2, and slicedArr3. The first index specified for slicedArr1 is -1, apparently an invalid index (since indexing begins from 0).

However, negative indexes are treated as indexes counting from the end of the array. For example, the element on index -1 is !, the element on index -2 is shot, and so on up to the first element. Therefore, the slicedArr1 stores the elements ('!', 'I', 'enjoyed', 'reading', this, 'shot').

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

In 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 myArr (5), no slicing takes place, and an empty slice is returned.

RELATED TAGS

perl
communitycreator
RELATED COURSES

View all Courses

Keep Exploring