Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: do $n exist in grep?

by Marshall (Canon)
on May 12, 2021 at 15:19 UTC ( [id://11132491]=note: print w/replies, xml ) Need Help??


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 +.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11132491]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-26 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found