Cookbook/Perl / List

来源:互联网 发布:淘宝链接怎么复制 编辑:程序博客网 时间:2024/06/07 08:57

Splice allows you to add, delete elements, or both, at any point in any array

# splice 从列表中切除元素, 列表值会发生改变# 表示从最后一个元素往回切 6 个元素, 如果超过列表大小,会提示: # Modification of non-creatable array value attemptesplice(@array, 0, 3) # 切除 0 , 1, 2, 3 四个元素map say, splice(@array, -6) 

To add some elements :

splice(@array, 2)  # will remove all the elements from $array[2] to the end of the list 

splice (expr, offset, length, list) : removes the elements designated by “offset” and “length” from an array, and replace them with the elements of list

e.g.,

my @array = (0, 1, 2, 3, 4, 5) ; splice(@array, @array - 3, 1, 6, 7) ; # will remove 3 and push 6, 7 between 2 and 4 splice(@array, $i, 1, $y)  # == $array[$i] = $y 

“`

0 0
原创粉丝点击