awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have this list that is basically what a List::MoreUtils mesh or zip output of a list would look like.

Looking all over there doesn't seem to be an unmesh or unzip function out there and my attempts look ugly. I know how many list were zipped into the input, so
my @test1 = qw( 1 1 2 2 3 3 4 4 5 5 6 6 7 7 ); my @test2 = qw( 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 ); # unzip @test1, we know to make it two lists my $unzipped = unzip( 2, @test1 ); $unzipped = [ [ 1, 2, 3, 4, 5, 6, 7 ], [ 1, 2, 3, 4, 5, 6, 7 ], ] # unzip @test2, we know to make it three lists my $unzipped = unzip( 3, @test2 ); $unzipped = [ [ 1, 2, 3, 4, 5, 6, 7 ], [ 1, 2, 3, 4, 5, 6, 7 ], [ 1, 2, 3, 4, 5, 6, 7 ], ]
Any ideas if "unzip" or "unmesh" exists or an elegant way to implement unzip?


Replies are listed 'Best First'.
Re: UnMesh or UnZip List
by AnomalousMonk (Archbishop) on Jun 27, 2016 at 22:49 UTC

    List::MoreUtils::part().

    Update: E.g.:

    c:\@Work\Perl>perl -wMstrict -le "use List::MoreUtils qw(part); ;; use Data::Dump qw(dd); ;; my @test = qw(1 1 2 2 3 3 4 4 5 5 6 6 7 7); ;; my $i = 0; my @unzip = part { $i++ % 2 } @test; dd \@unzip; ;; @test = qw(9 9 9 8 8 8 7 7 7 6 6 6 5 5 5 4 4 4 3 3 3); $i = 0; @unzip = part { $i++ % 3 } @test; dd \@unzip; " [[1 .. 7], [1 .. 7]] [ [9, 8, 7, 6, 5, 4, 3], [9, 8, 7, 6, 5, 4, 3], [9, 8, 7, 6, 5, 4, 3], ]
    The call to  part() is easily wrapped in a function call.


    Give a man a fish:  <%-{-{-{-<