and autovivification will create the sub-refs, though only for those elements that actually get assigned to. It's probably best to go ahead and specify the arrayrefs, but it could be a little cumbersome. It can be made simpler by a little tweak: the 2nd argument can be an arrayref or a number; if a number, partition creates that many sub-array(ref)s.my @r = partition { $_ % 3 } [], 0 .. 12;
Also note that the map cannot be replaced with ([]) x $receivers_ar because that will make three copies of the same ref.sub partition (&$@) { my $condition = shift; my $receivers_ar = shift; ref $receivers_ar or $receivers_ar = [ map [], 1..$receivers_ar ]; push @{ $receivers_ar->[ &$condition ] }, $_ for @_; @$receivers_ar } my @r = partition { $_ % 3 } 3, 0 .. 12;
In reply to Re^4: Splitting array into two with a regex
by Roy Johnson
in thread Splitting array into two with a regex
by jredburn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |