Perl的grep和地图有什么作用?

时间:2010-10-14 07:49:29

标签: perl

这段代码实际上做了什么?

@array = ( 'hai','hello','bar','foo' ) ;

print grep (/hai/ , @array );
print grep ("hai",@array ) ;
print map (/hai/ , @array );
print map ("hai",@array ) ;

2 个答案:

答案 0 :(得分:11)

它调用mapgrep函数。可以在perlfuncgrepmap条目中找到相关说明。

今天Google被打破了吗?

答案 1 :(得分:7)

尝试一下......

print grep (/hai/ , @array );
hai
# writes all elements from @array containing 'hai' in them

print grep ("hai",@array ) ;
haihellobarfoo
# writes all elements, because "hai" evaluates to true

print map (/hai/ , @array );
1
# writes 1 for the only element from the @array, that contains 'hai'

print map ("hai",@array ) ;
haihaihaihai
# maps 'hai' to each element from @array