in reply to Re^2: Splitting array into two with a regex
in thread Splitting array into two with a regex
To generalize it even further:
sub partition (&$@) { my $condition = shift; my $receivers_ar = shift; push @{ $receivers_ar->[ &$condition ] }, $_ for @_; @$receivers_ar } # Example 1: my( @good, @bad ); partition { /u/ ? 1 : 0 } [ \@good, \@bad ], qw( foo bar quux ); print "good=@good\n"; print "bad=@bad\n"; # Example 2: my @r = partition { $_ % 3 } [ [], [], [], ], 0 .. 12; for my $i ( 0 .. $#r ) { print "$i = @{$r[$i]}\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Splitting array into two with a regex
by dragonchild (Archbishop) on Nov 08, 2005 at 17:58 UTC | |
|
Re^4: Splitting array into two with a regex
by Roy Johnson (Monsignor) on Nov 08, 2005 at 16:33 UTC |