in reply to list to LoL

I'm still working on some code, but I think you could do it with a loop and splice.

Ok, here is my code:

@A=(1,2,3,4,5,6,7,8,9); # some array $n = 3; # some n while( @B = splice @A, $i++, $n ) { @A= ( [@B], @A ) }; @A = reverse @A; # To put it back in the right order.

And yes, that blows away the old @A.

Replies are listed 'Best First'.
RE:(2) list to LoL
by swiftone (Curate) on Jun 02, 2000 at 21:27 UTC
    Very true, but since you are dealing with a reference, splice would clobber your original list.

    The code for a splice loop should look something like:

    sub lolize{ my($size, $arrayref)=@_; my @returnarray=(); my $count=0; while($count<=$@arrayref){ push @returnarray, [splice(@$arrayref,$count, $count+$size-1)] +; $count+=$size; } return [@returnarray]; }
    This is untested, so this might "fill-in" your array to fill in the last list. To correct that, you say:
    push @returnarray, [splice(@$arrayref,$count, ($count+$size-1<=@$array +ref)? $count+size-1 :@$arrayref )];
    Hmm. There are probably some fencepost errors and such when $size=1 and suchlike, but this should let you play with it.