Count error in loop

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Count error in loop

amelia
With the following:

CfgBoard      = [1,3,0];
// ...
BoardTot        = 0;
BoardNum        = 0;
for (BoardNum=[0:1:2])
 {
  echo("CfgBoard",CfgBoard[BoardNum]);   // CfgBoard 1 3 0
  if (CfgBoard[BoardNum] != 0)
   {
    BoardTot = BoardTot + 1;                   // BoardTot count only 0->1
    echo("BoardTot1",BoardTot);
   }
  else                                                   // for debug
   {
    echo("BoardTot2",BoardTot);              // BoardTot is always cleared to 0
   }
   echo("BoardNum1",BoardNum);            // BoardTot during loop is OK
 }            
echo("BoardTot",BoardTot);                    // BoardTot is 0
echo("BoardNum",BoardNum);                 // BoardNum is 0


I am not able to count the CfgBoard contents;

amelia
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Count error in loop

MichaelAtOz
Administrator
Hi amelia,

Welcome to the forum.

You problem is 'BoardTot = BoardTot + 1; '.

Unlike Imperative languages, such as C, OpenSCAD is not an iterative language, as such the concept of x = x + 1  is not valid. Each 'loop' of the for() is a distinct scope, basically the for() build a tree of objects, each distinct from each other.

Get to understand this concept and you will understand the beauty of OpenSCAD.

Depending on what you want to do there are other ways, such as recursion, or the List Comprehensions.
Admin - PM me if you need anything,
or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Count error in loop

MichaelAtOz
Administrator
In reply to this post by amelia
You could adapt the example from the wiki, test for CfgBoard != 0, to build a list of non-zero boards, then use len() to count the members of the new list. There are prob a number of ways to do it.
[ for (i = list) if (condition(i)) i ]
    When the evaluation of the condition returns true, the expression i is added to the result list.

Example

list = [ for (a = [ 1 : 8 ]) if (a % 2 == 0) a ];
echo(list); // ECHO: [2, 4, 6, 8]

Note that the if element cannot be inside an expression, it should be at the top.

Example

// from the input list include all positive odd numbers
// and also all even number divided by 2

list = [-10:5];
echo([for(n=list) if(n%2==0 || n>=0) n%2==0 ? n/2 : n ]);
// ECHO: [-5, -4, -3, -2, -1, 0, 1, 1, 3, 2, 5]
// echo([for(n=list) n%2==0 ? n/2 : if(n>=0) n ]); // this would generate a syntactical error
Admin - PM me if you need anything,
or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Loading...