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

Re^2: Capturing regex from map

by Anonymous Monk
on Sep 14, 2013 at 06:41 UTC ( #1054060=note: print w/replies, xml ) Need Help??

in reply to Re: Capturing regex from map
in thread Capturing regex from map

I'd say map { /^online.*svc:(.*?):/; $1 || () }

just to be clear on what the expected transformation is. I feel that a regexp-match inside a map is fairly abstruse.

Replies are listed 'Best First'.
Re^3: Capturing regex from map
by Anonymous Monk on Sep 14, 2013 at 06:52 UTC
    Some other variants:
    map { /^online.*svc:(.*?):/; $1 // () } map { /^online.*svc:(.*?):/ and $1 or () } map { /^online.*svc:(.*?):/ ? $1 : () }
    The latter two are easy to extend if there are multiple captures. (The middle one is my favourite.)
Re^3: Capturing regex from map
by brianski (Novice) on Sep 15, 2013 at 17:28 UTC
    This is definitely clearer, I'd recommend it.

      I think you're wrong about clearer.

      But bedsides that, it does not do (and cannot me made to do), the same thing:

      @a = map{ m[(..)(.)]g } 'the quick brown fox', 'jumps over the lazy do +g';; print @a;; th e q u ic k b r ow n f o ju m ps ov e r t he la z y d @a = map{ m[(..)(.)]g; $1 || () } 'the quick brown fox', 'jumps over t +he lazy dog';; print @a;; th ju

      Perl is context sensitive. If you do not understand what that is, or try ignore it, you will never make best use of Perl.

      On the other hand, if you take a few hours or days to understand context sensitivity, the OPs code is clear, concise, and very readable.

      And very powerful. It requires many extra, contorted (and ultimately redundant) lines of code to achieve the same thing without using the effects of list context. It is a tool, that once you've realised its powerful simplicity, you will not want to do without.

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Well the OP had only one grouping and no /g modifier, such that it does the same thing.

        DB<120> @a = map { /(\w*a\w*)/;$1||() } `ls` => ( "babel", "emacs1000", "lanx", "lanx", "seahorse", "man", "Tracker", "virtual", ) DB<121> @a = map { /(\w*a\w*)/ } `ls` => ( "babel", "emacs1000", "lanx", "lanx", "seahorse", "man", "Tracker", "virtual", )


        FWIW: As already posted my approach to clarify whats happening is

        map { my @matches = /.../ ; @matches } LIST or even map { my @matches = /.../ } LIST

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054060]
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-07-25 03:43 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (363 votes). Check out past polls.