Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Prepend more elements to the b ...Join a list into a string usin ... (Berikutnya)
Functions for list data

Locate elements in a list test true against a given criterion

Daftar Isi

  • grep BLOCK LIST

  • grep EXPR,LIST

    This is similar in spirit to, but not the same as, grep(1) and itsrelatives. 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 thoseelements for which the expression evaluated to true. In scalarcontext, returns the number of times the expression was true.

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

    or equivalently,

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

    Note that $_ is an alias to the list value, so it can be used tomodify the elements of the LIST. While this is useful and supported,it can cause bizarre results if the elements of LIST are not variables.Similarly, grep returns aliases into the original list, much as a forloop's index variable aliases the list elements. That is, modifying anelement of a list returned by grep (for example, in a foreach, mapor another grep) actually modifies the element in the original list.This is usually something to be avoided when writing clear code.

    If $_ is lexical in the scope where the grep appears (because it hasbeen declared with my $_) then, in addition to being locally aliased tothe list elements, $_ keeps being lexical inside the block; i.e., itcan't be seen from the outside, avoiding any potential side-effects.

    See also map for a list composed of the results of the BLOCK or EXPR.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Prepend more elements to the b ...Join a list into a string usin ... (Berikutnya)