珀尔 | splice() - 多功能函数
在 Perl 中,splice()函数用于从数组中删除并返回一定数量的元素。可以插入元素列表来代替已删除的元素。
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.
例子:
#!/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
具有多个参数的情况:
案例一:拼接(@array)
如果传递了@array 但其余参数未传递,则清除整个数组并返回所有元素。但是,不会引发错误。
例子:
#!/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
案例2:拼接(@array,偏移)
如果@array 和offset 在没有指定length 和replacement_list 的情况下传递,则从offset 到末尾的所有元素都将被移除并返回。
例子:
#!/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
案例 3:拼接(@array,偏移量,长度)
如果指定了@array、偏移量和长度,则删除从偏移量开始的“长度”个元素。
例子:
#!/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
案例4:拼接(@array,偏移量,长度,replacement_list)
在这种情况下,'length' 个元素被删除并返回。 replacement_list 取而代之。
例子:
#!/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
笔记:
- 如果没有数组传递给 splice(),则会引发错误。
- 替换列表的元素数量不必等于已移除元素的数量。
例子:
#!/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");
输出: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
- 长度和偏移量可以是负数。
例子:
#!/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");
输出:splice(@arr, -3, 2): 0 1 2 3 4 7 splice(@arr1, 3, -2): 0 1 2 6 7
- 如果偏移量大于数组的长度,则将 replacement_list 附加到 @array 的末尾。
例子:
#!/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");
输出: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。
等价于使用 splice() 的 push():
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.
'list' 放在@array 的末尾。没有元素被移除/删除。
例子:
#!/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!
在上面的例子中,
- 最初,@array 具有以下元素:Geeks、for、Geeks。
- scalar(@array) 是数组的长度 (OR) 数组中的元素个数,即 3。
- 从@array[3] 开始的元素将被删除,但由于@array[3] 不存在,因此不会删除任何元素。
- 列表 ('Hello', 'There!') 将被插入到 @array 的末尾。现在,@array 包含以下元素:Geeks,for,Geeks.,Hello,There!
pop 用于移除并返回数组的最后一个元素
使用 splice() 等效于 pop():
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.
>> 从数组的最后一个元素开始删除一个元素并返回给 $pop。
例子:
#!/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.
在上面的例子中,
- 最初,@array 包含以下元素:“Geeks”、“for”、“Geeks”。
- 在 splice()函数中,从最后一个元素开始并包括最后一个元素的所有元素都被删除,即仅最后一个元素被删除并返回给 $pop。
- 现在,@array 具有以下元素:'Geeks'、'for'。而且,$pop 有元素:'Geeks'。
将数组中的所有元素向左移动一个块并删除并返回第一个元素称为 Shift。
等效于使用 splice() 的 shift():
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
>>从第一个元素开始并包括第一个元素的一个元素被删除并返回给 $removed。数组中的所有剩余元素自动向左移动一个索引。
>> 这与 pop() 类似,只是删除发生在另一端。
例子:
#!/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
在上面的例子中,
- 最初,@array 包含以下元素:“Geeks”、“for”、“Geeks”。
- 在 splice()函数中,从数组的左端删除一个元素。
- 现在,@array 包含以下元素:'for'、'Geeks.'。而且,$pop 有元素:'Geeks'。
在数组的左端插入给定的元素列表/数组称为 Unshift。
使用 splice() 等效于 unshift():
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.
>>insertion_list 的元素被插入到数组的开头。 @array 的所有现有元素都被推到右侧以容纳插入的元素。
例子:
#!/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.
在上面的例子中,
- 最初,@array 包含以下元素:“Geeks”、“for”、“Geeks”。
- 在 splice()函数中,@insertion_list 的元素被插入到@array 的开头。
- 现在,@array 包含以下元素:“This”、“is”、“Geeks”、“for”、“Geeks”。