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:
@array = (0..7);
print "Original Array: @array\n" ;
@array2 = splice ( @array , 2, 3, (a..c));
print ( "Elements of Updated \@array are @array\n" );
print ( "Removed elements are @array2" );
|
Output:
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:
@array = (0..7);
print "Original Array: @array\n" ;
@array2 = splice ( @array );
print ( "Updated Array: @array\n" );
print ( "Removed elements are: @array2" );
|
Output:
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:
@array = (0..7);
print "Original Array: @array\n" ;
@array2 = splice ( @array , 3);
print ( "Updated Array: @array\n" );
print ( "Removed elements are: @array2" );
|
Output:
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:
@array = (0..7);
print "Original Array: @array\n" ;
@array2 = splice ( @array , 3, 2);
print ( "Updated Array: @array\n" );
print ( "Removed elements are: @array2" );
|
Output:
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:
@array = (0..7);
print "Original Array: @array\n" ;
@array2 = splice ( @array , 3, 2, (a, b));
print ( "Updated Array: @array\n" );
print ( "Removed elements are: @array2" );
|
Output:
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:
@array = (0..7);
print "Original Array: @array\n" ;
@array2 = splice ( @array , 3, 2, (a..d));
print ( "Updated Array: @array\n" );
print ( "Removed elements are: @array2" );
|
Output:
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:
@arr = (0..7);
@arr1 = (0..7);
splice ( @arr , -3, 2);
print ( 'splice(@arr, -3, 2): ' . "@arr \n" );
splice ( @arr1 , 3, -2);
print ( 'splice(@arr1, 3, -2): ' . "@arr1" );
|
Output:
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:
@array = (0..7);
print "Original Array: @array\n" ;
splice ( @array , 9, 2, (a..d));
print ( "Updated Array: @array\n" );
|
Output:
Original Array: 0 1 2 3 4 5 6 7
Updated Array: 0 1 2 3 4 5 6 7 a b c d
push using splice()
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:
@array = ( 'Geeks' , 'for' , 'Geeks.' );
print "Original Array: @array\n" ;
splice ( @array , scalar ( @array ), 0, ( 'Hello' , 'There!' ));
print ( "Updated Array: @array\n" );
|
Output:
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 using splice()
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:
@array = ( 'Geeks' , 'for' , 'Geeks.' );
print "Original Array: @array\n" ;
$pop = splice ( @array , -1);
print ( "Updated Array: @array\n" );
print ( "Removed element is $pop" );
|
Output:
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.’
shift using splice()
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:
@array = ( 'Geeks' , 'for' , 'Geeks.' );
print "Original Array: @array\n" ;
$removed = splice ( @array , 0, 1);
print ( "Updated Array: @array\n" );
print ( "Removed element is $removed" );
|
Output:
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.’
unshift using splice()
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:
@array = ( 'Geeks' , 'for' , 'Geeks.' );
print "Original Array: @array\n" ;
@insertion_list = ( 'This' , 'is' );
splice ( @array , 0, 0, @insertion_list );
print ( "Updated Array: @array\n" );
|
Output:
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.’
Last Updated :
21 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...