Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Is there a default array for reg exp memory variables?

by tobyink (Canon)
on Feb 05, 2012 at 22:35 UTC ( [id://951998]=note: print w/replies, xml ) Need Help??


in reply to Is there a default array for reg exp memory variables?

The return value of the "=~" operator (if called in list context) is the array you desire.

use Data::Dumper; if (my @r = "foo bar baz" =~ /(foo) (bar) (baz)/) { print Dumper \@r } __END__ $VAR1 = [ 'foo', 'bar', 'baz' ];

You say:

Can we get hold of that array explicitly somehow when we are not in list context?

... so clearly you already know the above. Why not just use list context? There is rarely any reason to explicitly avoid it when regexp matching. Please explain what you're trying to actually do, and why performing matches in list context is insufficient.

Depending on what you're trying to do, you could consider using named captures, which get stored into the hash %-. If you name the captures appropriately, you could assemble them into an array...

sub get_caps () { my @caps; my $i = 1; while (exists $-{'cap'.$i}) { push @caps, $-{'cap'.$i++}->[0]; } @caps; } use Data::Dumper; if (scalar("foo bar baz" =~ /(?<cap1>foo) (?<cap2>bar) (?<cap3>baz)/)) { my @r = get_caps; print Dumper \@r; } __END__ $VAR1 = [ 'foo', 'bar', 'baz' ];

Though obviously that involves modifying the regular expression itself to add the named captures. And it needs a non-archaic version of Perl (at least 5.10).

Replies are listed 'Best First'.
Re^2: Is there a default array for reg exp memory variables?
by korpenkraxar (Sexton) on Feb 05, 2012 at 23:05 UTC

    I located the concept named capture buffers five minutes ago, then I go over here and see that you have already posted code using it and Eliya has also provided great feedback. Thanks! Virtual beer for both of you!

    I just haven't seen reg exps in list context in enough examples or code to actually realize its power until I stumbled across it in Effective Perl Programming. I agree it is the way to go but I still think we could have use for a named default array. Is there a reason for not having it in there?

      but I still think we could have use for a named default array. Is there a reason for not having it in there?

      One reason: If it existed, the safest thing to do would be to copy it into your own array to ensure that the data it contained didn't get overwritten by a subsequent regex invocation before you were finished with it.

      What would be the point of the global default array if the only thing you could safely do with it is copy it to somewhere else, when you can assign the results directly to that other place?


      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.

      The start of some sanity?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found