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

...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

Replies are listed 'Best First'.
Re^4: global regex returning a list of arrays?
by AnomalousMonk (Archbishop) on Feb 21, 2010 at 06:25 UTC

    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' ];