Why use this syntax at all? What does it give, except that all the elements of the array / arrays are added to the new array? Maybe there is some nuance in this that I don’t know about ?!

Example:

@result = grep { $_ } @arr1, @arr2; 
  • such syntax will not put in @result undefs, '' and 0. this is a simple and not quite correct filter of defaulted values - nörbörnën
  • @ nörbörnën, eh! That's how it is! Everything, understood, thanks a lot. I simply from two arrays, and received in the same that was in 2 arrays. - 0-Level UNIX Monk
  • 2
    do not forget that he will cut 0. better to say defined $_ && $_ ne '' - nörbörnën
  • 2
    Better not to compare with empty line. A faster option is to check its length: length $_ . Although a shorter entry would be: length , - since it works with $_ if the arguments are not passed explicitly. So you can not write: defined && length , - since length also makes a check on the defined - Eugen Konkov
  • one
    @nörbörnën: Why? the question is not asked about it - Eugen Konkov

1 answer 1

grep used to filter the elements of an array.

Usually when filtering, we set the condition of which elements are suitable for us:

 grep{ $_ > 10 } @arr; # Всё, что больше 10 

In your case, you will only have TRUE elements left: not zero, not empty string, not undefined