perl的grep,map用法及区别
2013-10-15 11:48
507 查看
1、map
对数组的每个元素计算语句值,最后将得到的最终值组成一个数组返回,$_为当前元素值
格式:
1-1、 @arr=map 单条语句,@number;
1-2、 @arr=map {多条语句} @number;
例子:
@arr=map $_+1,1..3; // @arr=(2,3,4)
@arr=map {$_=$_+1;$_+1} 1..3; // @arr=(3,4,5)
2、grep
对数组的轮询,语句(语句块)为真,则对将该元素加入到返回的数组中(返回的数组初始为空),$_为当前元素值
格式:
1-1、 @arr=grep 单条语句,@number;
1-2、 @arr=grep {多条语句} @number;
例子:
@arr=map $_%2==1,1..3; // @arr=(1,3)
@arr=map { $_%2==0} 1..3; // @arr=(2)
3、转换
例子:
将找出一个1-100中所有的奇数
grep方式
@arr=grep $_%2==1, 1..100;
map方式
@arr=map {();if($_%2==1){$_;}else{();}} 1..100;
http://zk1878.iteye.com/blog/1164906
对数组的每个元素计算语句值,最后将得到的最终值组成一个数组返回,$_为当前元素值
格式:
1-1、 @arr=map 单条语句,@number;
1-2、 @arr=map {多条语句} @number;
例子:
@arr=map $_+1,1..3; // @arr=(2,3,4)
@arr=map {$_=$_+1;$_+1} 1..3; // @arr=(3,4,5)
2、grep
对数组的轮询,语句(语句块)为真,则对将该元素加入到返回的数组中(返回的数组初始为空),$_为当前元素值
格式:
1-1、 @arr=grep 单条语句,@number;
1-2、 @arr=grep {多条语句} @number;
例子:
@arr=map $_%2==1,1..3; // @arr=(1,3)
@arr=map { $_%2==0} 1..3; // @arr=(2)
3、转换
例子:
将找出一个1-100中所有的奇数
grep方式
@arr=grep $_%2==1, 1..100;
map方式
@arr=map {();if($_%2==1){$_;}else{();}} 1..100;
http://zk1878.iteye.com/blog/1164906
相关文章推荐
- perl map-map用法示例和grep的用法
- perl grep 和 map 简单用法
- perl中grep,sort,map用法总结
- perl中grep,map用法总结
- Perl中map和grep 的使用区别
- perl中grep,sort,map用法总结
- perl中grep,sort,map用法总结(转)
- perl中grep,map用法总结
- Java中的Set、List、Map的用法与区别
- Collection,List,Set和Map用法和区别
- c++ map.count 和map.find 的用法区别
- Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法
- c++ list, vector, map, set 区别与用法比较
- perl 用grep map求交集、并集、补集
- c++ list, vector, map, set 区别与用法比较
- map flatmap mappartition flatMapToPair四种用法区别
- List,Set,Map用法以及区别
- perl中的map和grep
- c++ list, vector, map, set 区别与用法比较
- Collection,List,Set和Map用法和区别