tags:

views:

114

answers:

2

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 ) ;
+11  A: 

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?

musiKk
:-( A search for "grep map hai" didn't turn up anything helpful.
mobrule
It now turns up this question. ;)
musiKk
+6  A: 

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
eumiro