grep的主要目的:
挑选出列表里符合表达式或者符合正则模式或者符合子程序要求的项,用于替换foreach的繁琐操作
my @input_numbers=(1,2,3,4,5,6);my @bigger_than_3=grep $_>3,@input_numbers;
my @input_numbers=(1,2,3,4,5,6);my @bigger_than_3=grep &bigger($_),@input_numbers;sub bigger{ if($_[0]>3) {1} else {0};}
my @input_numbers=(1,2,3,4,5,6,44);my @end_with_4=grep /4$/,@input_numbers;
阅读(610) | 评论(0) | 转发(0) |