listperlpartitionperl-module

How to split a list in two over a condition?


Is there a grep-like function that splits a list into both the part that matches the expression, as grep does, and also its complement?

Of course it would be very simple to write such a function, but I'm interested to know if it already exists and would be more conveniently included from any of the List::Util-like modules.


Solution

  • I found the answer browsing through List::MoreUtils right after posting the question (my apologies). It's part:

    Partitions LIST based on the return value of BLOCK which denotes into which partition the current value is put. Returns a list of the partitions thusly created. Each partition created is a reference to an array.

    my $i = 0;
    my @part = part { $i++ % 2 } "a".."h";   # Returns [qw( a c e g )], [qw( b d f h )]