Perl | splice() - The Versatile Function
Last Updated :
21 Feb, 2019
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:
perl
#!/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");
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:
perl
#!/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");
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:
perl
#!/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");
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:
perl
#!/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");
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:
perl
#!/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");
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:
perl
#!/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 four elements i.e. (a..d)
@array2 = splice(@array, 3, 2, (a..d));
print("Updated Array: @array\n");
# Removed elements
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:
perl
#!/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 6
splice(@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 5
splice(@arr1, 3, -2);
# Printing Second splice()
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:
perl
#!/usr/bin/perl
# an array of numbers from 0 to 7
@array = (0..7);
# Original Array
print "Original Array: @array\n";
# offset is greater than the
# length of the array.
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:
perl
#!/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");
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:
perl
#!/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");
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:
perl
#!/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");
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:
perl
#!/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");
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.'
Similar Reads
Perl | split() Function
split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression(pattern), a group of characters or on undefined value etc.. The best thing abou
8 min read
Perl | sprintf() Function
sprintf() function in Perl uses Format provided by the user to return the formatted string with the use of the values in the list. This function is identical to printf but it returns the formatted string instead of printing it. Syntax: sprintf Format, List Returns: a formatted scalar string Example
1 min read
Perl | shift() Function
shift() function in Perl returns the first value in an array, removing it and shifting the elements of the array list to the left by one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop. This function returns undef if the array is empt
2 min read
Perl | srand() Function
The srand() function in Perl helps rand() function to generate a constant value each time the program is run. This srand() function uses the same parameter each time the rand() function is called for getting constant random value. Syntax: srand(seed) Parameters: seed : It is an integer value. Return
3 min read
Perl | substr() function
substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified. A replacement string can also be passe
2 min read
Perl | rindex() Function
rindex() function in Perl operates similar to index() function, except it returns the position of the last occurrence of the substring (or pattern) in the string (or text). If the position is specified, returns the last occurrence at or before that position. Syntax: # Searches pat in text from given
2 min read
Perl | unshift() Function
unshift() function in Perl places the given list of elements at the beginning of an array. Thereby shifting all the values in the array by right. Multiple values can be unshift using this operation. This function returns the number of new elements in an array. Syntax: unshift(Array, List)Returns: Nu
2 min read
Perl | Useful String functions
A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (â) or double quote (â). Perl provid
3 min read
Perl | List Functions
A list in Perl is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol whereas list varia
4 min read
Perl | keys() Function
keys() function in Perl returns all the keys of the HASH as a list. Order of elements in the List need not to be same always, but, it matches to the order returned by values and each function. Syntax: keys(HASH) Parameter: HASH: Hash whose keys are to be printed Return: For scalar context, it return
1 min read