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.
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.
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.
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.
- Perl | Array pop() Function
- Perl | Useful Array functions
- Perl | Reverse an array
- Perl | Getting the Number of Elements of an Array
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl vs C/C++
- Perl | tell() Function
- Formats in Perl
- Perl | abs() function
- Packages in Perl
- Perl | exp Function
- Perl | hex Function
- Perl | eq operator
- Perl | le operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : shubham_singh