Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Capturing regex from map

by thezip (Vicar)
on Sep 13, 2013 at 17:02 UTC ( #1053968=note: print w/replies, xml ) Need Help??

in reply to Capturing regex from map


my @online_services = map { /^online.*svc:(.*?):/; $1 } `svcs`;

What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011

Replies are listed 'Best First'.
Re^2: Capturing regex from map
by LanX (Saint) on Sep 13, 2013 at 17:14 UTC
    > try my @online_services = map { /^online.*svc:(.*?):/; $1 } `svcs`;

    not equivalent:

    DB<217> map { /(1\d*)/;$1 } 5..15 => (undef, undef, undef, undef, undef, 10, 11, 12, 13, 14, 15) DB<218> map { /(1\d*)/ } 5..15 => (10, 11, 12, 13, 14, 15)

    Cheers Rolf

    ( addicted to the Perl Programming Language)


    I think you meant:

    DB<225> map { @matches=/(1\d*)/;@matches} 5..15 => (10, 11, 12, 13, 14, 15)

      That's what I get for not testing! </sheepish grin>

      What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: Capturing regex from map
by Anonymous Monk on Sep 14, 2013 at 06:41 UTC
    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.

      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.)
      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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2023-09-24 11:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found