Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: global regex returning a list of arrays?

by LanX (Saint)
on Feb 20, 2010 at 22:25 UTC ( [id://824439]=note: print w/replies, xml ) Need Help??


in reply to Re: global regex returning a list of arrays?
in thread global regex returning a list of arrays?

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

Replies are listed 'Best First'.
Re^3: global regex returning a list of arrays?
by LanX (Saint) on Feb 20, 2010 at 23:09 UTC
    ...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://824439]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found