Hello,
Trying to create an array with sub-arrays of size given by the elements of the original array like this:
[ [ 1 ], [ 2, 3 ], [ 4, 5, 6, 7 ], [ 8, 9, 10, 11, 12, 13, 14, 15 ] ]
from:
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
I came up with this:
(
a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; x = []; c = Pseq(a,1).asStream;
i = 0;
while ( { (a.at(i)) > 0 }, { (j=a.at(i)); k = [c.nextN(j)]; x = x ++ k; a=a.shift((a.at(i)).neg);});
x.postln
)
Which works, but in an example like this:
(
a = [ 3, 4, 2, 11, 8, 3, 1, 0, 7, 9, 6, 4, 4, 8, 1, 6, 5, 11, 8, 9 ]; x = []; c = Pseq(a,1).asStream;
i = 0;
while ( { (a.at(i)) > 0 }, { (j=a.at(i)); k = [c.nextN(j)]; x = x ++ k; a=a.shift((a.at(i)).neg);});
x.postln
)
I get : [ [ 3, 4, 2 ], [ 11, 8, 3, 1, 0, 7, 9, 6, 4, 4, 8 ], [ 1 ], [ 6, 5, 11, 8, 9, nil ] ]
which is supposed to be due to the .shift method I’m using. So my questions are:
- How could I chop off the “nil” results at the end?
- Is there a better way to get the result I need?
- and is there a way to get on the post window more elements instead of the “…etc…” when larger amounts of elements need to be displayed?
Thanks, I appreciate your help.
JF