Help DS with FOR and WHILE

Hello to all,
I’m trying to make a DS but do not know well yet the syntax, so I’m trying things.

This is what I would get

Can you give me some advice and tell me where to find a guide to the Dynamo internal language?
Thanks so much.

Not sure what you’re trying to achieve, but try this (not tested):

hdfh = [    Imperative]
{
x = {0, 2, 0, 5, 8};

a = {};
for (i in GetKeys(x))
{
l = 0;
a[i] = {};
while (l <= x[i])
{
a[i][l] = x[i];

l = l+1;
}
}

}

To achieve your intent in the second image you posted, you can try this:

hdfh = [Imperative]
{
	x = {0, 0, 0, 2, 3, 4 ,4 ,4, 5, 7 ,7 ,0, 0, 3, 1, 1, 1};

	a = { };
	subList = 0;
	currentIndex = 0;
	for (i in GetKeys(x) )
	{
		a[subList][currentIndex] = x[i];
		currentIndex = currentIndex +1;

		if (x[i] != x[i+1])
		{
			subList = subList + 1;
			currentIndex = 0;
		}
	}
	return = a;
};

5 Likes

Thanks Thomas, is perfect and clear!:grinning:

Something slightly different:

FOR LOOP

[Imperative]
{
x = {0, 0, 0, 2, 3, 4 ,4 ,4, 5, 7 ,7 ,0, 0, 3, 1, 1, 1};

   a = {};
   l = 0;
   m = 0;

   for (i in GetKeys(x))
   {
      if (x[i] == x[i+1])
      {
         a[l][m] = x[i];
         m = m + 1;
      }
      else
      {
         a[l][m] = x[i];
         l = l + 1;
         m = 0;
      }
   }
   return = a;
};

WHILE LOOP

[Imperative]
{
x = {0, 0, 0, 2, 3, 4 ,4 ,4, 5, 7 ,7 ,0, 0, 3, 1, 1, 1};

   a = {};
   n = 0;
   l = 0;
   m = 0;

   while (n < Count(x))
   {
      if (x[n] == x[n+1])
      {
         a[l][m] = x[n];
         m = m + 1;
      }
      else
      {
         a[l][m] = x[n];
         l = l + 1;
         m = 0;
      }
      n = n + 1;
   }
   return = a;
};

1 Like