Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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

by haukex (Bishop)
on May 23, 2019 at 07:41 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2020-07-12 20:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found