Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: global regex returning a list of arrays?

by AnomalousMonk (Archbishop)
on Feb 20, 2010 at 17:32 UTC ( [id://824403]=note: print w/replies, xml ) Need Help??


in reply to global regex returning a list of arrays?

Perhaps more scalable, but still involving an explicit loop:

>perl -wMstrict -le "use List::MoreUtils qw(natatime); use Data::Dumper; my $s = 'ab123cd456ef789gh'; my $triad = natatime 3, $s =~ m{ (\d) (\d) (\d) }xmsg; while (my @three = $triad->()) { print Dumper \@three; } " $VAR1 = [ '1', '2', '3' ]; $VAR1 = [ '4', '5', '6' ]; $VAR1 = [ '7', '8', '9' ];

Replies are listed 'Best First'.
Re^2: global regex returning a list of arrays?
by LanX (Saint) on Feb 20, 2010 at 22:25 UTC
    Ok if we include MoreUtils we could use part which does what I wanted without an explicit while:

    DB<41> use List::MoreUtils "part" DB<42> $a=join " ",0..5 DB<43> x part {my $i++/2} $a =~/(\w) (\w)/g 0 ARRAY(0x90c6430) 0 0 1 1 1 ARRAY(0x90c68a0) 0 2 1 3 2 ARRAY(0x90c68b0) 0 4 1 5

    but after considering MoreUtils it's only a small step to define a specialized functional solution fold (&) on one's own...

     grep { } fold { $a =~/(\w) (\w)/g }.

    Cheers Rolf

      ...it's only a small step to define a specialized functional solution fold (&) on one's own...

      hmm not as easy as I thought

      $a=join " ",0..7; sub fold (&) { my @a; push @a, [$1,$2] while $_[0]->(); @a}; print Dumper fold { $a =~/(\w) (\w)/g };

      seems like $1,$2 can't leave the scope of the coderef...

      Cheers Rolf

      UPDATE: c&p error in code

        This seems a bit hackish, but...

        >perl -wMstrict -le "$a = join ' ', 0 .. 7; sub fold (&) { local $_; my @a; push @a, $_ while $_[0]->(); return @a; } use Data::Dumper; print Dumper fold { return $_ = $a =~ /(\w) (\w)/g ? [ $1, $2 ] : undef; }; " $VAR1 = [ '0', '1' ]; $VAR2 = [ '2', '3' ]; $VAR3 = [ '4', '5' ]; $VAR4 = [ '6', '7' ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found