Perl | splice() – The Versatile Function
In Perl, the splice() function is used to remove and return a certain number of elements from an array. A list of elements can be inserted in place of the removed elements.
Syntax: splice(@array, offset, length, replacement_list)
Parameters:
- @array – The array in consideration.
- offset – Offset of removal of elements.
- length – Number of elements to be removed starting at offset(including offset).
- replacement_list- The list of elements that takes the place of the removed elements.
Example:
#!/usr/bin/perl # an array of numbers from 0 to 7 @array = (0..7); # Original array print "Original Array: @array\n"; # splice() replaces elements from # 2 to 4 with a to c @array2 = splice(@array, 2, 3, (a..c)); # Printing the Updated Array print("Elements of Updated \@array are @array\n"); # array2 contains elements removed # from array i.e. 2, 3 and 4 print("Removed elements are @array2"); |
Original Array: 0 1 2 3 4 5 6 7 Elements of Updated @array are 0 1 a b c 5 6 7 Removed elements are 2 3 4
Cases with multiple parameters:
Case 1: splice(@array)
If the @array is passed but the rest of the parameters are not, the entire array is cleared and all elements returned. However, no error is raised.
Example:
#!/usr/bin/perl # an array of numbers from 0 to 7 @array = (0..7); # Original Array print "Original Array: @array\n"; # All the elements of @array are removed @array2 = splice(@array); print("Updated Array: @array\n");#Blank Line # Removed elements print("Removed elements are: @array2"); |
Original Array: 0 1 2 3 4 5 6 7 Updated Array: Removed elements are: 0 1 2 3 4 5 6 7
Case 2: splice(@array, offset)
If the @array and offset are passed without specifying length and replacement_list, all the elements from the offset to the end are removed and returned.
Example:
#!/usr/bin/perl # an array of numbers from 0 to 7 @array = (0..7); # Original Array print "Original Array: @array\n"; # All the elements of @array starting # from @array[3] are removed @array2 = splice(@array, 3); print("Updated Array: @array\n"); # Removed elements print("Removed elements are: @array2"); |
Original Array: 0 1 2 3 4 5 6 7 Updated Array: 0 1 2 Removed elements are: 3 4 5 6 7
Case 3: splice(@array, offset, length)
If the @array, the offset, and the length is specified, ‘length’ number of elements starting from offset are removed.
Example:
#!/usr/bin/perl # an array of numbers from 0 to 7 @array = (0..7); # Original Array print "Original Array: @array\n"; # Two elements of @array starting from # @array[3] are removed i.e 3 and 4 @array2 = splice(@array, 3, 2); print("Updated Array: @array\n"); # Removed elements print("Removed elements are: @array2"); |
Original Array: 0 1 2 3 4 5 6 7 Updated Array: 0 1 2 5 6 7 Removed elements are: 3 4
Case 4: splice(@array, offset, length, replacement_list)
In this case, ‘length’ number of elements are removed and returned. replacement_list takes their place.
Example:
#!/usr/bin/perl # an array of numbers from 0 to 7 @array = (0..7); # Original Array print "Original Array: @array\n"; # Two elements of @array starting from # @array[3] are removed i.e 3 and 4 and # replaced by a list of elements i.e. (a, b) @array2 = splice(@array, 3, 2, (a, b)); print("Updated Array: @array\n"); # Removed elements print("Removed elements are: @array2"); |
Original Array: 0 1 2 3 4 5 6 7 Updated Array: 0 1 2 a b 5 6 7 Removed elements are: 3 4
Note:
- If no array is passed to splice(), an error is raised.
- The number of elements of replacement list need not be equal to the number of removed elements.
Example:
#!/usr/bin/perl# an array of numbers from 0 to 7@array= (0..7);# Original Arrayprint"Original Array: @array\n";# Two elements of @array starting from# @array[3] are removed i.e 3 and 4 and# replaced by four elements i.e. (a..d)@array2=splice(@array, 3, 2, (a..d));print("Updated Array: @array\n");# Removed elementsprint("Removed elements are: @array2");chevron_rightfilter_noneOutput:Original Array: 0 1 2 3 4 5 6 7 Updated Array: 0 1 2 a b c d 5 6 7 Removed elements are: 3 4
- The length and the offset can be negative.
Example:
#!/usr/bin/perl# Two arrays of numbers from 0 to 7@arr= (0..7);@arr1= (0..7);# Two elements are removed from the# 3rd element from the end i.e. 5 and 6splice(@arr, -3, 2);# Printing First splice()print('splice(@arr, -3, 2): '."@arr \n");# Elements from 3 to 2nd element from# the end are removed i.e. 3, 4 and 5splice(@arr1, 3, -2);# Printing Second splice()print('splice(@arr1, 3, -2): '."@arr1");chevron_rightfilter_noneOutput:splice(@arr, -3, 2): 0 1 2 3 4 7 splice(@arr1, 3, -2): 0 1 2 6 7
- If the offset is more than the length of the array, replacement_list is attached at the end of the @array.
Example:
#!/usr/bin/perl# an array of numbers from 0 to 7@array= (0..7);# Original Arrayprint"Original Array: @array\n";# offset is greater than the# length of the array.splice(@array, 9, 2, (a..d));print("Updated Array: @array\n");chevron_rightfilter_noneOutput:Original Array: 0 1 2 3 4 5 6 7 Updated Array: 0 1 2 3 4 5 6 7 a b c d
Inserting an element at the end of an array is termed as Push.
Equivalent of push() using splice():
Syntax: splice(@array, scalar(@array), 0, list)
Parameters:
- @array – The array in consideration.
- scalar(@array)- It is the length of the array.
- 0 – It removes no element.
- list – The list of elements to be pushed at the end of the array.
The ‘list’ is placed at the end of @array. No elements are removed/deleted.
Example:
#!/usr/bin/perl # Initializing an array @array = ('Geeks', 'for', 'Geeks.'); # Original Array print "Original Array: @array\n"; # push function splice(@array, scalar(@array), 0, ('Hello', 'There!')); # Printing the updated Array print("Updated Array: @array\n"); |
Original Array: Geeks for Geeks. Updated Array: Geeks for Geeks. Hello There!
In the above example,
- Initially, @array has the elements: Geeks, for, Geeks.
- scalar(@array) is the length of the array (OR) number of elements in the array, which is 3.
- Elements starting from @array[3] are to be removed but since @array[3] is not present, no elements will be removed.
- The list (‘Hello’, ‘There!’) will be inserted at the end of @array. Now, @array has the elements: Geeks, for, Geeks., Hello, There!
Pop is used to remove and return the last element of the array
Equivalent of pop() using splice():
Syntax: $pop = splice(@array, -1)
Parameters:
- $pop – The popped element.
- @array – The array in consideration
- -1 – Removal of all the elements starting from the last one.
>> One element is removed starting from the last element of the array and is returned to $pop.
Example:
#!/usr/bin/perl # Initializing an array @array = ('Geeks', 'for', 'Geeks.'); # Original array print "Original Array: @array\n"; # last element is removed and returned $pop = splice(@array, -1); # Printing the Updated Array print("Updated Array: @array\n"); # $pop contains removed element # from array i.e. last element print("Removed element is $pop"); |
Original Array: Geeks for Geeks. Updated Array: Geeks for Removed element is Geeks.
In the above example,
- Initially, @array has the elements: ‘Geeks’, ‘for’, ‘Geeks.’
- In the splice() function, all elements starting from and including the last element are removed i.e. only the last element is removed and returned to $pop.
- Now, @array has the elements: ‘Geeks’, ‘for’. And, $pop has the element: ‘Geeks.’
To move all the elements in an array to the left by one block and removing and returning the first element is termed as Shift.
Equivalent of shift() using splice():
Syntax: $removed = splice(@array, 0, 1)
Parameters:
- $removed – The popped element i.e. the first one.
- @array – The array in consideration
- 0 – Removal of the elements starts from the first one.
- 1 – One element is to be removed starting from and including the first one i.e. only the first element is removed and returned to $removed
>>One element starting from and including the first element is removed and returned to $removed. All the remaining elements in the array are automatically moved to the left by one index.
>>This is similar to pop() except that the removal takes place at the opposite end.
Example:
#!/usr/bin/perl # Initializing an array @array = ('Geeks', 'for', 'Geeks.'); # Original array print "Original Array: @array\n"; # shift function $removed = splice(@array, 0, 1); # Printing the Updated Array print("Updated Array: @array\n"); # $removed contains removed element # from array i.e. first element print("Removed element is $removed"); |
Original Array: Geeks for Geeks. Updated Array: for Geeks. Removed element is Geeks
In the above example,
- Initially, @array has the elements: ‘Geeks’, ‘for’, ‘Geeks.’
- In the splice() function, one element is removed from the left end of the array.
- Now, @array has the elements: ‘for’, ‘Geeks.’. And, $pop has the element: ‘Geeks.’
Inserting a given list/array of elements at the left end of an array is termed as Unshift.
Equivalent of unshift() using splice():
Syntax: splice(@array, 0, 0, insertion_list)
Parameters:
- @array – The array in consideration
- 0 – Insertion takes place at the 0th index i.e beginning of the array.
- 0 – No elements are removed or deleted.
- insertion_list- The elements to be inserted.
>>The elements of insertion_list are inserted at the beginning of the array. All the existing elements of the @array are pushed to the right to accommodate the inserted elements.
Example:
#!/usr/bin/perl # Initializing an array @array = ('Geeks', 'for', 'Geeks.'); # Original array print "Original Array: @array\n"; @insertion_list = ('This', 'is'); # unshift function splice(@array, 0, 0, @insertion_list); # Printing the Updated Array print("Updated Array: @array\n"); |
Original Array: Geeks for Geeks. Updated Array: This is Geeks for Geeks.
In the above example,
- Initially, @array has the elements: ‘Geeks’, ‘for’, ‘Geeks.’
- In the splice() function, elements of @insertion_list are inserted at the beginning of @array.
- Now, @array has the elements: ‘This’, ‘is’, ‘Geeks, ‘for’, ‘Geeks.’
Recommended Posts:
- Perl | oct() Function
- Perl | cos() Function
- Perl | sin() Function
- Perl | tell() Function
- Perl | ord() Function
- Perl | exp Function
- Perl | hex Function
- Perl | chr() Function
- Perl | uc() Function
- Perl | log() Function
- Perl | abs() function
- Perl | each() Function
- Perl | int() function
- Perl | wantarray() Function
- Perl | sort() Function
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.



