Perl | Array Slices

In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable.
Arrays can store any type of data and that data can be accessed in multiple ways. These values can be extracted by placing $ sign before the array and storing the index value of the element to be accessed within the square brackets.
For Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Define an array
@arr = (1, 2, 3);
  
# Accesing and printing first 
# element of an array
print "$arr[0]\n";
  
# Accesing and printing second
# element of an array
print "$arr[1]\n";

chevron_right


This method of extracting array elements can be used only to extract one element at a time, which might become confusing when there is a long list of elements to be accessed. For Example, if the list contains 100 elements and we need to extract 20 elements from Index ‘a’ to Index ‘b’, then this method will create a confusion. In order to avoid such kind of situation, Perl provides a method of array slicing. This can be used to access a range of array elements.

Slicing of an Array

Array slicing is done to access a range of elements in an array in order to ease the process of accessing a multiple number of elements from the array. This can be done in two ways:

  • Passing multiple index values
  • Using range operator

Passing multiple Index values:
Array slicing can be done by passing multiple index values from the array whose values are to be accessed. These values are passed to the array name as the argument. Perl will access these values on the specified indices and perform the required action on these values.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Perl program to implement the use of Array Slice
@array = ('Geeks', 'for', 'Geek');
  
# Using slicing method
@extracted_elements = @array[1, 2];
  
# Printing the extracted elements
print"Extracted elements: "
     "@extracted_elements";

chevron_right


Output:

Extracted elements: for Geek

This method of passing multiple indexes becomes a bit complicated when a large number of values are to be accessed.

Using Range Operator
Range operator[..] can also be used to perform the slicing method in an array by accessing a range of elements whose starting and ending index are given within the square brackets separated by range operator(..). This method is more feasible as it can print elements within a long range of elements, as compared to passing multiple parameters.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Perl program to implement the use of Array Slice
@array = ('Geeks', 'for', 'Geek', 'Welcomes', 'You');
  
# Using range operator for slicing method
@extracted_elements = @array[1..3];
  
# Printing the extracted elements
print"Extracted elements: "
     "@extracted_elements";

chevron_right


Output:

Extracted elements: for Geek Welcomes

This method of slicing the Array to access elements is used widely to perform multiple operations on the array. Operations like, pushing elements, printing elements of array, deleting elements, etc.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.