GREP

grep BLOCK LIST
grep EXPR,LIST

Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to TRUE. In a scalar context, returns the number of times the expression was TRUE.

@foo = grep(!/^#/, @bar); # weed out comments

or equivalently,

@foo = grep {!/^#/} @bar; # weed out comments

Note that, since $_ is a reference into the list value, it can be used to modify the elements of the array. While this is useful and supported, it can cause bizarre results if the LIST is not a named array.

Back to functions