Perl | splice () - Универсальная функция
Опубликовано: 2 Февраля, 2022
В 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.
Example:
#!/usr/bin/perl # an array of numbers from 0 to 7 @array = (0..7); # Original array print "Original Array: @array
" ; # 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
" ); # 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