Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: accessing the result of a match as an array

by AnomalousMonk (Bishop)
on Dec 24, 2009 at 22:35 UTC ( #814299=note: print w/replies, xml ) Need Help??


in reply to Re: accessing the result of a match as an array
in thread accessing the result of a match as an array

And just for the record:

>perl -wMstrict -le "print pop @{[ 'hello awesome' =~ /(el).*(om)/ ]}; " om

Update: And with 5.10 regex one can also do:

>perl -wMstrict -le "'hello awesome' =~ /(?<m>el).*(?<m>om)/; print $-{m}[-1]; " om

(See %- in perlvar.)

Replies are listed 'Best First'.
Re^3: accessing the result of a match as an array
by JadeNB (Chaplain) on Dec 24, 2009 at 23:04 UTC

    What's the advantage of your latter snippet over

    perl -e '"hello awesome" =~ /(el).*(om)/ and print $2;'
    or, if you don't like to count,
    perl -e '"hello awesome" =~ /(el).*(om)/ and print $^N;'

    By the way, while I'm nattering about linking, [doc://perlvar#%-] (but not [doc://perlvar#%25-], which over-escapes) facilitates the OP's laziness: % .

    UPDATE: It seems to me that the link text for [doc://perlvar#%-] should be '%-', but it's actually '% '. Is this a bug? (Oops, that's right: - is parsed as a word separator, as in [doc://perlrun#Location-of-Perl].)

      What's the advantage of your latter snippet ...

      None. In the context of the OP, it's essentially a Stupid Regex Trick. But then, in the same context
          pop @{[ whatever ]}
      is essentially a stupid array dereference trick.

      Update: ikegami nailed the reason why.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-05-14 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (146 votes). Check out past polls.

    Notices?