Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: map and regexp. An newbie question

by anneli (Pilgrim)
on Oct 19, 2011 at 06:56 UTC ( #932307=note: print w/replies, xml ) Need Help??

in reply to Re: map and regexp. An newbie question
in thread map and regexp. An newbie question

If you're using a more recent Perl, you can use the /r option to s///:

my %hash = map {$_, $_ =~ s/a/X/gr} @array;

Replies are listed 'Best First'.
Re^3: map and regexp. An newbie question
by davido (Archbishop) on Oct 19, 2011 at 07:06 UTC

    Did you try that?

    The /r modifier (Perl 5.14) only solves one of the problems (it doesn't alter the original). It doesn't solve the second problem that the $_ =~ s/a/X/gr construct is not being evaluated in list context, and therefore returns the number of substitution matches, not the modified string.

    Apparently I didn't. ;) -- lousy excuse in a followup node below.


      No, but I got lucky! See perlop:

      If the /r (non-destructive) option is used then it runs the substitution on a copy of the string and instead of returning the number of substitutions, it returns the copy whether or not a substitution occurred. The original string is never changed when /r is used. The copy will always be a plain string, even if the input is an object or a tied variable.

      This applies even when /g is being used (otherwise what use would /r be, being that it's non-destructive?).

      (Edit: to clarify, no, I didn't try it, but I did upon being chastened, found that it did work, and ran to docs to find out why!)

        Yep, you're correct.

        I created a test snippet, and uploaded it to my Linux computer (where I have 5.14 running) via sFTP, since the /r modifier isn't available on my Windows/Strawberry Perl 5.12 computer. Then I ran it by SSH'ing into the computer. And in so doing, mistakenly uploaded and tested a version that I hadn't added the /r modifier to. So when I thought I was checking, I wasn't.

        The docs.... And here I'm usually the one to have them practically memorized.

        Good job!!! Great humor.

        It reminds me of one of the great quotes from Programming Perl. The topic of discussion actually was what various Perl functions return in list and scalar context, which seems quite fitting to this conversation.

        "In general, Perl functions do exactly what you want, unless you want consistency."

        -- Larry Wall; Tom Christiansen; Jon Orwant. Programming Perl, 3rd Edition (Kindle Locations 15841-15842). O'Reilly Media.


Re^3: map and regexp. An newbie question
by nando (Acolyte) on Oct 19, 2011 at 15:35 UTC

    I knew that having an assignment operation, the behavior of Perl was logical ... But I was sure, also, that there was a way to get it simple and transparent ...Thank you, anneli!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932307]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-01-16 22:41 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (192 votes). Check out past polls.