Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Why do I need a local variable in map function

by haukex (Archbishop)
on May 23, 2019 at 07:41 UTC ( [id://11100403]=note: print w/replies, xml ) Need Help??


in reply to Why do I need a local variable in map function

s/// returns the number of substitutions made, so that's what that map block is returning. Although it's generally not considered good practice to modify the original list by modifying or assigning to $_ in a map block, the first version could be written as map { $_ = quotemeta($_) . '$'; s/\\\*/.*/g; $_ } to achieve the desired result. Alternatively, as of Perl 5.14, you can say s///r to have the regex return the modified string, e.g. map { my $x = quotemeta($_) . '$'; $x =~ s/\\\*/.*/gr }.

Update: Note that your second regex includes /g, while your first one does not; I've added it to both my examples.

Update 2: Or even just map { quotemeta($_).'$' =~ s/\\\*/.*/gr } or map { "\Q$_\E\$" =~ s/\\\*/.*/gr }

Replies are listed 'Best First'.
Re^2: Why do I need a local variable in map function
by Anonymous Monk on May 23, 2019 at 09:35 UTC
    Thanks for the perfect answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11100403]
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 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found