What does this code actually do?
@array = ( 'hai','hello','bar','foo' ) ;
print grep (/hai/ , @array );
print grep ("hai",@array ) ;
print map (/hai/ , @array );
print map ("hai",@array ) ;
What does this code actually do?
@array = ( 'hai','hello','bar','foo' ) ;
print grep (/hai/ , @array );
print grep ("hai",@array ) ;
print map (/hai/ , @array );
print map ("hai",@array ) ;
It invokes the map
and grep
functions. A description of which can be found in the perlfunc entries for grep and map.
Is Google broken today?
Try it out...
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