http://www.perlmonks.org?node_id=11132491


in reply to do $n exist in grep?

Grep could have been called "filter". But it is: "get regular expression".
Use grep when you think about getting a subset of an array.
use strict; use warnings; my @x = ("aaBxyz", "..Bxys", "bbxyzzy", "xxAx"); # for each element in the array @x, # pass that element to the array @result if # if the 3rd character is either A or B # followed by at least one character. my @result = grep {/^..(A|B)./}@x; print "$_\n" for @result; =prints aaBxyz ..Bxys xxAx =cut
Yes, in this case $1 will be either A or B if the regex is True.
I can show that, but I don't want to confuse you.
Here $1 is not relevant.

Ok, on second thought, here we go...

#ok, with a weird example of $1 @result = grep{ print "$1 $_\n" if /^..(A|B)./; /^..(A|B)./}@x; =prints B aaBxyz B ..Bxys A xxAx =cut @result is the same as above # grep {} executes the code within the brackets and passes the input # to @result depending upon the true/false value of the last statement +.