perl使用

1.操作perl 数组的pop,push,shift,unshift区别

pop:删除并返回数组的最后一个元素。

my @names = ('Foo', 'Bar', 'Baz');
my $last_one = pop @names;

print "$last_one\n";  # Baz
print "@names\n";     # Foo Bar

若数组为空,返回undef.

push:在数组的后面添加一个或多个值.

my @names = ('Foo', 'Bar');
push @names, 'Moo';
print "@names\n";     # Foo Bar Moo

my @others = ('Darth', 'Vader');
push @names, @others;
print "@names\n";     # Foo Bar Moo Darth Vader

shift:使整个数组向左移动,那么第一个元素就会从数组上“掉下来”,成为函数的返回值,这样操作以后,数组会减少一个值。

my @names = ('Foo', 'Bar', 'Moo');
my $first = shift @names;
print "$first\n";     # Foo
print "@names\n";     # Bar Moo

unshift:shift的反函数。会传入一个或多个值(或者0个值)并把它放在数组的开头,将其他元素向右移动,可以传单个的标量,那么这个值会成为数组的第一个元素,也可以传递第二个数组,那么第二个数组的所有元素会复制到主数组的开头,并将其他元素向高索引方向移动。

my @names = ('Foo', 'Bar');
unshift @names, 'Moo';
print "@names\n";     # Moo Foo Bar

my @others = ('Darth', 'Vader');
unshift @names, @others;
print "@names\n";     # Darth Vader Moo Foo Bar

2.$_=~s/A/B/g      将内容A全部替换成B

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值