NAME
grep - locate elements in a list test true against a given criterion
SYNOPSIS
grep BLOCK LIST
grep EXPR,LIST
DESCRIPTION
This is similar in spirit to, but not the same as,
grep(1) and its relatives. In
particular, it is not limited to using regular expressions.
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, because $_ 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. Similarly, grep returns aliases into the original list, much like the way that a for loop's index variable aliases the list elements. That is, modifying an element of a list returned by grep (for example, in a
foreach, map() or another grep()) actually modifies the element in the original list.
See also map for an array composed of the results of the BLOCK or EXPR.
DISCLAIMER
We are painfully aware that these documents may contain incorrect links and misformatted HTML. Such bugs lie in the automatic translation process that automatically created the hundreds and hundreds of separate documents that you find here. Please do not report link or formatting bugs, because we cannot fix per-document problems. The only bug reports that will help us are those that supply working patches to the installhtml or pod2html programs, or to the Pod::HTML module itself, for which I and the entire Perl community will shower you with thanks and praises.If rather than formatting bugs, you encounter substantive content errors in these documents, such as mistakes in the explanations or code, please use the perlbug utility included with the Perl distribution.
- --Tom Christiansen, Perl Documentation Compiler and Editor
Return to the Perl Documentation Index.
Return to the Perl Home Page.
