Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Using regex in Map function

by spandox (Novice)
on Feb 05, 2013 at 21:14 UTC ( [id://1017284]=note: print w/replies, xml ) Need Help??


in reply to Re: Using regex in Map function
in thread Using regex in Map function

************** Bad Program ****
my @in = qw(a b c d e f g h); print "Start: ",@in,"\n","***RUNNING MAP***\n\n"; my @out = map { s/.$/x/} @in; print "in : ",@in,"\n"; print "out: ",@out,"\n"
************* Output ******
Start: abcdefgh ***RUNNING MAP*** in : xxxxxxxx out: 11111111
*********************
Comment #1:
Use "r" feature on regex
************** fixed Program ****
my @in = qw(a b c d e f g h); print "Start: ",@in,"\n","***RUNNING MAP***\n\n"; my @out = map { s/.$/x/r} @in; print "in : ",@in,"\n"; print "out: ",@out,"\n"
************* Output ******
Start: abcdefgh ***RUNNING MAP*** in : abcdefgh out: xxxxxxxx
****************
Comment #2

But what if you REALLY wanted the return value of the regex - and not modify the original output? (why? I don't know)
Localize the input...

************** fixed Program ****
my @in = qw(a b c d e f g h); print "Start: ",@in,"\n","***RUNNING MAP***\n\n"; my @out = map { s/.$/x/} my @temp = @in; print "in : ",@in,"\n"; print "out: ",@out,"\n"
************* Output ******
Start: abcdefgh ***RUNNING MAP*** in : abcdefgh out: 11111111

Replies are listed 'Best First'.
Re^3: Using regex in Map function
by merlyn (Sage) on Feb 08, 2013 at 18:40 UTC
    You're replying to my note from 2007 with a feature that has only been available for a few releases. Nice.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      However, I found the suggestion of using the /r modifier very helpful in 2016!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found