perl-5.38.0
grep BLOCK LIST
grep EXPR,LIST

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.

これは grep(1) とその親類と同じようなものですが、同じではありません。 特に、正規表現の使用に制限されません。

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 scalar context, returns the number of times the expression was true.

LIST の個々の要素に対して、BLOCK か EXPR を評価し ($_ は、ローカルに個々の要素が設定されます) 、 その要素のうち、評価した式が真となったものからなるリスト値が返されます。 スカラコンテキストでは、式が真となった回数を返します。

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

or equivalently,

あるいは等価な例として:

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

Note that $_ is an alias to the list value, so it can be used to modify 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 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. This is usually something to be avoided when writing clear code.

$_ は、LIST の値へのエイリアスですので、LIST の要素を 変更するために使うことができます。 これは、便利でサポートされていますが、 LIST の要素が変数でないと、おかしな結果になります。 同様に、grep は元のリストへのエイリアスを返します; for ループの インデックス変数がリスト要素のエイリアスであるのと同様です。 つまり、grep で返されたリストの要素を (foreach, map, または他の grep で)修正すると元のリストの要素が変更されます。 これはきれいなコードを書くときには普通は回避されます。

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

BLOCK や EXPR の結果をリストの形にしたい場合は map を 参照してください。