Indexing error in functions (while- and for loops)

Browsed the first page of most recent posts but this doesn’t seem to be a big issue…

I’m now at a point where I master some Dynamo functions in designscript, so code rather than the input/output blocks, and try to use functions wherever I have to repeat a few lines of code. But since a little while, I keep getting an indexation error whenever I use a while() loop and try to put the result into a list. I’ve tried the most common steps to find the error, but it drives me nuts to have to close and re-open the file every time.

So this is basically how I learned it: I first declare i = 0, then start a loop to test the value of i against another parameter. It increases at the end of each loop (i = i + 1) (by the way… whatever happened to i++?) but before that I put the result into a collection (list[i] = result).
The error occurs at the last step, when I’m using i as an index for the returned list. My best translation from Dutch: ‘The index is outside the range. It cannot be negative and must be smaller than the size of the collection.’

Here’s a quick sample in which I try to generate ten panels of 600 by 1000 units:

<span style=“color: #8c5eaa;”>def</span> PlacePanels(midpoint)

{
surfaces = {};

startpoint = midpoint;
vector = <span style=“color: #2e998f;”>Vector</span>.<span style=“color: #417693;”>ByCoordinates</span>(<span style=“color: #2585e5;”>1</span>, <span style=“color: #2585e5;”>0</span>, <span style=“color: #2585e5;”>0</span>);
vecPerp = vector.<span style=“color: #417693;”>Rotate</span>(<span style=“color: #2e998f;”>Vector</span>.<span style=“color: #417693;”>ByCoordinates</span>(<span style=“color: #2585e5;”>0</span>, <span style=“color: #2585e5;”>0</span>, <span style=“color: #2585e5;”>1</span>), <span style=“color: #2585e5;”>90</span>);
path;
profile;
surface;

i = <span style=“color: #2585e5;”>0</span>;

<span style=“color: #8c5eaa;”>return</span> = [<span style=“color: #8c5eaa;”>Imperative</span>]
{

<span style=“color: #8c5eaa;”>while</span> (i < <span style=“color: #2585e5;”>10</span>)
{
path = <span style=“color: #2e998f;”>Line</span>.<span style=“color: #417693;”>ByStartPointDirectionLength</span>(startpoint, vector, <span style=“color: #2585e5;”>600</span>);
profile = <span style=“color: #2e998f;”>Line</span>.<span style=“color: #417693;”>ByStartPointDirectionLength</span>(startpoint, vecPerp, <span style=“color: #2585e5;”>1000</span>);
surface = <span style=“color: #2e998f;”>Surface</span>.<span style=“color: #417693;”>BySweep</span>(path, profile);
surfaces[i] = surface;

i = i + <span style=“color: #2585e5;”>1</span>;
startpoint = path.<span style=“color: #417693;”>EndPoint</span>;

}
<span style=“color: #8c5eaa;”>return</span> = i;
}
};

The same happens in a for() loop:

<span style=“color: #8c5eaa;”>def</span> PlacePanels(midpoint)
{
surfaces = <span style=“color: #2585e5;”>0</span>…<span style=“color: #2585e5;”>9</span>;

startpoint = midpoint;
vector = <span style=“color: #2e998f;”>Vector</span>.<span style=“color: #417693;”>ByCoordinates</span>(<span style=“color: #2585e5;”>1</span>, <span style=“color: #2585e5;”>0</span>, <span style=“color: #2585e5;”>0</span>);
vecPerp = vector.<span style=“color: #417693;”>Rotate</span>(<span style=“color: #2e998f;”>Vector</span>.<span style=“color: #417693;”>ByCoordinates</span>(<span style=“color: #2585e5;”>0</span>, <span style=“color: #2585e5;”>0</span>, <span style=“color: #2585e5;”>1</span>), <span style=“color: #2585e5;”>90</span>);
path;
profile;
surface;

<span style=“color: #8c5eaa;”>return</span> = [<span style=“color: #8c5eaa;”>Imperative</span>]
{

<span style=“color: #8c5eaa;”>for</span>(surface <span style=“color: #8c5eaa;”>in</span> surfaces)
{
path = <span style=“color: #2e998f;”>Line</span>.<span style=“color: #417693;”>ByStartPointDirectionLength</span>(startpoint, vector, <span style=“color: #2585e5;”>600</span>);
profile = <span style=“color: #2e998f;”>Line</span>.<span style=“color: #417693;”>ByStartPointDirectionLength</span>(startpoint, vecPerp, <span style=“color: #2585e5;”>1000</span>);
surface = <span style=“color: #2e998f;”>Surface</span>.<span style=“color: #417693;”>BySweep</span>(path, profile);
startpoint = path.<span style=“color: #417693;”>EndPoint</span>;

}
<span style=“color: #8c5eaa;”>return</span> = surfaces;
}
};

Does anyone have a clue what’s causing this, or how to fix it? I’m starting to run out of options here.
Kind regards, Bram

Here’s the code again but without the annoying formatting…

 

def PlacePanels(midpoint)
{
surfaces = {};

startpoint = midpoint;
vector = Vector.ByCoordinates(1, 0, 0);
vecPerp = vector.Rotate(Vector.ByCoordinates(0, 0, 1), 90);
path;
profile;
surface;

i = 0;

return = [Imperative]
{

while(i < 10)
{
path = Line.ByStartPointDirectionLength(startpoint, vector, 600);
profile = Line.ByStartPointDirectionLength(startpoint, vecPerp, 1000);
surface = Surface.BySweep(path, profile);
startpoint = path.EndPoint;

surfaces[i] = surface;

i = i + 1;

}
return = surfaces;
}
};

Yay, this sorted itself out by downloading and installing the latest build (0.8.1.1530).