perl中数组函数:delete和grep

一、delete函数
 

1.作用:从数组中删除指定的元素
 2.格式:delete $array[index]
 3.实例:#delete
@fruits=("apple","banana","berry","orange");
print "1:@fruits"."\n";

delete $fruits[1];
print "2:@fruits"."\n";

print "3:$fruits[1]"."\n";

delete $fruits[5];
print "4:@fruits"."\n";

$size=@fruits;
print "5:".$size;

4.结果:
1:apple banana berry orange
2:apple  berry orange
3:
4:apple  berry orange
5:4

5.总结:
(1)index的值是从0开始,当index大于数组长度时,函数delete对数组没影响
(2)元素被删除后,该位置上的值是undefined的

二、grep函数
 1.作用:按某种模式expr去匹配数组array中的每个元素,匹配成功的元素组成一个新的数组,返回该新数组
 2.格式:grep(expr,array);
 3.实例:
@words=("tomataes","tomorrow","patatoes","phantom","Tommy");
$count=grep(/tom/i,@words);
@items=grep(/tom/i,@words);

print "1.$count\n";
print "2.@items";

4.结果:
1.4
2.tomataes tomorrow phantom Tommy

5.总结:
(1)当返回的新数组赋值给一个标量时,该变量保存了新数组的元素个数
(2)当返回的新数组赋值给一个数组时,该变量保存了新数组的所有元素
(3)i选项忽略大小写
原文见:http://blog.sina.com.cn/s/blog_5d1edf6a0100upih.html
原文地址:https://www.cnblogs.com/yangyongzhi/p/2687279.html