in reply to Syntactically cool list of lists

I thought about using the natatime function from List::MoreUtils. It returns an iterator however, which is a bit clumsy in this context, so I looked at the source for natatime. I always forget about splice, but there it was :-)

FWIW, this is what I whipped up:

#!/usr/bin/perl -l use strict; sub partn($@) { my $n = shift; my @l = @_; my @r; push @r, [splice( @l, 0, $n )] while @l; return @r; } ## examples my @r = (1,5,7,9,32,197,8,4,5); my @r2 = partn 2, @r; my @r3 = partn 3, @r; my @r4 = partn 4, @r; use Data::Dumper; $Data::Dumper::Indent = 0; print Dumper \@r2; print Dumper \@r3; print Dumper \@r4; __END__ $VAR1 = [[1,5],[7,9],[32,197],[8,4],[5]]; $VAR1 = [[1,5,7],[9,32,197],[8,4,5]]; $VAR1 = [[1,5,7,9],[32,197,8,4],[5]];

Replies are listed 'Best First'.
Re^2: Syntactically cool list of lists
by ikegami (Patriarch) on Dec 21, 2006 at 00:26 UTC
    Doesn't seem to be that clumsy.
    use List::MoreUtils qw( natatime ); my $iter = natatime $l, @a; while (my @row = $iter->()) { push @b, \@row; }

    Update: Switched to using a while loop, since the while statement modifier doesn't work here.

      I was hoping someone would post something that iterates.

      -Paul