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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
@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?


In reply to Re: accessing the result of a match as an array by JadeNB
in thread accessing the result of a match as an array by intuited

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2021-05-06 12:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Perl 7 will be out ...





      Results (74 votes). Check out past polls.

      Notices?