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

Re: accessing the result of a match as an array

by JadeNB (Chaplain)
on Dec 24, 2009 at 22:09 UTC ( #814297=note: print w/replies, xml ) Need Help??

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

@matches = "hello awesome" =~ /(el).*(om)/; print pop @awesome;
but this is clearly unacceptable.

Particularly since you match into @matches, but pop @awesome. :-) As

prototype "CORE::pop"; # => ;\@
indicates, if pop takes any argument at all, then it must be an array (not a list)—in common parlance, it must start with a @ sigil. (In fact, its suitability for feeding to pop is one of the ways that people usually advise to distinguish an array from a list.) The reason for this, in turn, is (I think—but this is a difficult point, so I make it small so as to avoid giving offence if I am mistaken) exactly the one you have hit on—a list is not a data structure, in the sense that it is essentially internal to the interpreter and not meant for the user to be slinging around via such tactics as naming it, or, especially, mutating it (which is what pop does).

Since you clearly have no need for the array/list (because you don't want to name it), it's not important that you actually mutate it; so why not just index into it?

print ( ( "hello awesome" =~ /(el).*(om)/ )[-1] );
(Since I always forget them, I'll point out that the parentheses are necessary to avoid parsing as ( print ("hello awesome" =~ /(el).*(om)/) )[-1], which is, as perl will gently remind you, a syntax error.)

UPDATE: By the way, [doc://perldata] and [doc://scalar] become, respectively, perldata and scalar.
UPDATE 2: On further thought, I realise that I've never understood this error:

Type of arg 1 to pop must be array (not list assignment) at -e line 1, + near "/(el).*(om)/)"
That is, I understand that there is a difference between an array and a list assignment; but, since a list assignment returns the array being assigned to:
$ perl -e '( my @a = (1) ) = (2); print "@a\n";' 2
I don't understand why pop complains. I suspect it's a parsing problem; maybe one of the internals experts could be enticed to say for sure?

Replies are listed 'Best First'.
Re^2: accessing the result of a match as an array
by AnomalousMonk (Archbishop) on Dec 24, 2009 at 22:35 UTC

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

      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.

Re^2: accessing the result of a match as an array
by intuited (Novice) on Dec 26, 2009 at 04:09 UTC

    Hey, thanks. That helped to clarify it a bit. I actually was not aware of the ( match ) [ index ] syntax. I tried some similar stuff but never got it quite right, and wondered if I was still in the right ballpark at all.

    That's pretty much what I was looking for in the clean practical solution that I suspected the existence of. Also yeah thanks for pointing out my typing gaffe with the @awesome. And the doc:// shortcuts are great to know about, actually I see that there are a lot of interesting things happening in that section.

    I am still curious about the guts of it, ie what you mentioned in your second update, but will for now simply accept that perl works in mysterious ways. Thanks again!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2022-11-29 20:55 GMT
Find Nodes?
    Voting Booth?