Count error in loop

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

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
|

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 - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

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
|

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 - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

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
|

Re: Count error in loop

Baxi
Hello,

can this List Comprehension only count itself?

I tried without success to get this working to count the number of cubes:


/list = [for (a=[1,2,3,4,5]) {
        translate ([a*2,2,0]) cube (1, center=true);
        a}];

echo (list);/


This is working (but no cubes made at all...):

/list = [for (a=[1,2,3,4,5]) //{
// translate ([a*2,2,0]) cube (1, center=true);
        a
//}
];

echo (list);/



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Count error in loop

nophead
List comprehensions can only appear in lists and lists are expressions. You can't mix in modules. They can only appear in statements.

You can do this though:

list = [for (a=[1:5]) a];

echo(list);

for(a = list)
  translate ([a*2,2,0]) cube (1, center=true);

On 13 June 2018 at 17:20, Baxi <[hidden email]> wrote:
Hello,

can this List Comprehension only count itself?

I tried without success to get this working to count the number of cubes:


/list = [for (a=[1,2,3,4,5]) {
        translate ([a*2,2,0]) cube (1, center=true);
        a}];

echo (list);/


This is working (but no cubes made at all...):

/list = [for (a=[1,2,3,4,5]) //{
//      translate ([a*2,2,0]) cube (1, center=true);
        a
//}
];

echo (list);/



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Count error in loop

Baxi
Thx, I understand this.

Doing it in this way I would need to create a few lists.

I solved it in this way:

/count1 = [for (a=[1,2,3,4,5]) "x"];
for (a=[1,2,3,4,5]) {
        translate ([a*2,2,0]) cube (1, center=true);
}

count2 = [for (a=[6,7,8,9,10]) "y"];
for (a=[6,7,8,9,10]) {
        translate ([a*2,4,0]) cube (1, center=true);
}


echo (count1); //ECHO: ["x", "x", "x", "x", "x"]
echo (count2); //ECHO: ["y", "y", "y", "y", "y"]
echo ("No. of cubes: ", len(count1)+len(count2)); //ECHO: "No. of cubes: ",
10/



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Count error in loop

nophead
I don't understand why you need to count the cubes. The number produced is just the length of your lists. Why make another list of x's?

list1 = [for (a=[1,2,3,4,5]) a];
for(a = list1)
  translate ([a*2,2,0]) cube (1, center=true);

list2 = [for (a=[6,7,8,9,10]) a];
for (a=list2)
        translate ([a*2,4,0]) cube (1, center=true);

echo ("No. of cubes: ", len(list1)+len(list2));


On 13 June 2018 at 18:14, Baxi <[hidden email]> wrote:
Thx, I understand this.

Doing it in this way I would need to create a few lists.

I solved it in this way:

/count1 = [for (a=[1,2,3,4,5]) "x"];
for (a=[1,2,3,4,5]) {
        translate ([a*2,2,0]) cube (1, center=true);
}

count2 = [for (a=[6,7,8,9,10]) "y"];
for (a=[6,7,8,9,10]) {
        translate ([a*2,4,0]) cube (1, center=true);
}


echo (count1); //ECHO: ["x", "x", "x", "x", "x"]
echo (count2); //ECHO: ["y", "y", "y", "y", "y"]
echo ("No. of cubes: ", len(count1)+len(count2)); //ECHO: "No. of cubes: ",
10/



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Count error in loop

Baxi
My original -for- counting looks for example like this:

x=[-4.66,0,4.66],y=[-4.66,4.66],z=[-4.66,0,4.66])

a lot of such kinds....


nophead wrote

> I don't understand why you need to count the cubes. The number produced is
> just the length of your lists. Why make another list of x's?
>
> list1 = [for (a=[1,2,3,4,5]) a];
> for(a = list1)
>   translate ([a*2,2,0]) cube (1, center=true);
>
> list2 = [for (a=[6,7,8,9,10]) a];
> for (a=list2)
>         translate ([a*2,4,0]) cube (1, center=true);
>
> echo ("No. of cubes: ", len(list1)+len(list2));
>
>
> On 13 June 2018 at 18:14, Baxi &lt;

> service@

> &gt; wrote:
>
>> Thx, I understand this.
>>
>> Doing it in this way I would need to create a few lists.
>>
>> I solved it in this way:
>>
>> /count1 = [for (a=[1,2,3,4,5]) "x"];
>> for (a=[1,2,3,4,5]) {
>>         translate ([a*2,2,0]) cube (1, center=true);
>> }
>>
>> count2 = [for (a=[6,7,8,9,10]) "y"];
>> for (a=[6,7,8,9,10]) {
>>         translate ([a*2,4,0]) cube (1, center=true);
>> }
>>
>>
>> echo (count1); //ECHO: ["x", "x", "x", "x", "x"]
>> echo (count2); //ECHO: ["y", "y", "y", "y", "y"]
>> echo ("No. of cubes: ", len(count1)+len(count2)); //ECHO: "No. of cubes:
>> ",
>> 10/
>>
>>
>>
>> --
>> Sent from: http://forum.openscad.org/
>>
>> _______________________________________________
>> OpenSCAD mailing list
>>

> Discuss@.openscad

>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Count error in loop

nophead
You can still make those into lists of coordinates and use the length as the count.

list = [ for(x=[-4.66,0,4.66],y=[-4.66,4.66],z=[-4.66,0,4.66])  [x, y, z]];

echo(len(list)); // should be 18

for(p = list) translate(p) cube();



On 13 June 2018 at 19:04, Baxi <[hidden email]> wrote:
My original -for- counting looks for example like this:

x=[-4.66,0,4.66],y=[-4.66,4.66],z=[-4.66,0,4.66])

a lot of such kinds....


nophead wrote
> I don't understand why you need to count the cubes. The number produced is
> just the length of your lists. Why make another list of x's?
>
> list1 = [for (a=[1,2,3,4,5]) a];
> for(a = list1)
>   translate ([a*2,2,0]) cube (1, center=true);
>
> list2 = [for (a=[6,7,8,9,10]) a];
> for (a=list2)
>         translate ([a*2,4,0]) cube (1, center=true);
>
> echo ("No. of cubes: ", len(list1)+len(list2));
>
>
> On 13 June 2018 at 18:14, Baxi &lt;

> service@

> &gt; wrote:
>
>> Thx, I understand this.
>>
>> Doing it in this way I would need to create a few lists.
>>
>> I solved it in this way:
>>
>> /count1 = [for (a=[1,2,3,4,5]) "x"];
>> for (a=[1,2,3,4,5]) {
>>         translate ([a*2,2,0]) cube (1, center=true);
>> }
>>
>> count2 = [for (a=[6,7,8,9,10]) "y"];
>> for (a=[6,7,8,9,10]) {
>>         translate ([a*2,4,0]) cube (1, center=true);
>> }
>>
>>
>> echo (count1); //ECHO: ["x", "x", "x", "x", "x"]
>> echo (count2); //ECHO: ["y", "y", "y", "y", "y"]
>> echo ("No. of cubes: ", len(count1)+len(count2)); //ECHO: "No. of cubes:
>> ",
>> 10/
>>
>>
>>
>> --
>> Sent from: http://forum.openscad.org/
>>
>> _______________________________________________
>> OpenSCAD mailing list
>>

> Discuss@.openscad

>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Count error in loop

Baxi
Yes Thx.

Works fine and the source looks much nicer.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Count error in loop

Troberg
In reply to this post by MichaelAtOz
MichaelAtOz wrote
> Get to understand this concept and you will understand the beauty of
> OpenSCAD.

That's one way of saying it. Another way of saying it is "Get to understand
this concept and you'll be able to avoid some very annoying pitfalls which
makes this otherwise very lovely product harder to use.".

Don't get me wrong, I love OpenSCAD with all my heart, but this is just
plain annoying.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org