Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by haukex (Chancellor)
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2019-09-17 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (215 votes). Check out past polls.

    Notices?