Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: global regex returning a list of arrays?

by Corion (Patriarch)
on Feb 20, 2010 at 14:40 UTC ( [id://824387]=note: print w/replies, xml ) Need Help??


in reply to global regex returning a list of arrays?

perlvar lists @- and @+, but you still need a while loop to capture them:

#!/usr/local/bin/perl use strict; use Data::Dumper; my $str = 'abacadabra'; my @matches; push @matches, [ map { substr $str, $-[$_], $+[$_]-$-[$_] } 1..$#+ ] while $str =~ /([ac])([bd])/g; print Dumper \@matches;

Replies are listed 'Best First'.
Re^2: global regex returning a list of arrays?
by LanX (Saint) on Feb 20, 2010 at 15:06 UTC
    yeah I knew this trick with the index-array, thx,!

    But it's not really comfortable to need to substr... :(

    Cheers Rolf

      BTW: couldn't find an array holding the values of $1,$2,... in the perldocs !?!

      Probably not much help in this particular case (and you still need to loop to capture), but 5.10 offers the %+ and %- predefined hashes for named captures (see perlvar) in place of an array of all capture groups:

      >perl -wMstrict -le "my $s = '12ab34cd56ef78'; my @pairs; push @pairs, [ $+{A}, $+{B} ] while $s =~ m{ (?<A>[[:alpha:]]) (?<B>[[:alpha:]]) }xmsg; use Data::Dumper; print Dumper \@pairs; " $VAR1 = [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e', 'f' ] ];
        Probably not much help in this particular case...

        actually it's getting better now! :D

        ....it's possible to take the values of the hash!

         push @pairs, [ values %+ ] while $s =~ m{ (?<A>[[:alpha:]]) (?*[[:alpha:]]) }xmsg;

        unfortunately the hash is not set when the groupings are not named ... :(

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found