Cookbook/Perl / List

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

# splice 从列表中切除元素, 列表值会发生改变
# 表示从最后一个元素往回切 6 个元素, 如果超过列表大小,会提示: 
# Modification of non-creatable array value attempte
splice(@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 

“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值