A
amelia
Fri, Apr 21, 2017 7:14 AM
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
--
View this message in context: http://forum.openscad.org/Count-error-in-loop-tp21339.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
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
--
View this message in context: http://forum.openscad.org/Count-error-in-loop-tp21339.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Fri, Apr 21, 2017 7:30 AM
Hi amelia,
Welcome to the forum.
You problem is 'BoardTot = BoardTot + 1; '.
Unlike Imperative languages
https://en.wikipedia.org/wiki/Imperative_programming , 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
https://en.wikipedia.org/wiki/Recursion_%28computer_science%29 , or the
List Comprehensions
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/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!
View this message in context: http://forum.openscad.org/Count-error-in-loop-tp21339p21340.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Hi amelia,
Welcome to the forum.
You problem is 'BoardTot = BoardTot + 1; '.
Unlike Imperative languages
<https://en.wikipedia.org/wiki/Imperative_programming> , 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
<https://en.wikipedia.org/wiki/Recursion_%28computer_science%29> , or the
List Comprehensions
<https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/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!
--
View this message in context: http://forum.openscad.org/Count-error-in-loop-tp21339p21340.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Fri, Apr 21, 2017 7:46 AM
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!
View this message in context: http://forum.openscad.org/Count-error-in-loop-tp21339p21341.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
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!
--
View this message in context: http://forum.openscad.org/Count-error-in-loop-tp21339p21341.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
B
Baxi
Wed, Jun 13, 2018 4:20 PM
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/
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/
NH
nop head
Wed, Jun 13, 2018 4:48 PM
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 service@frankbaxmann.de 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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
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 <service@frankbaxmann.de> 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
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
B
Baxi
Wed, Jun 13, 2018 5:14 PM
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/
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/
NH
nop head
Wed, Jun 13, 2018 5:37 PM
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 service@frankbaxmann.de 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@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
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 <service@frankbaxmann.de> 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@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
B
Baxi
Wed, Jun 13, 2018 6:04 PM
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 <
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
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 <
> service@
> > 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/
NH
nop head
Wed, Jun 13, 2018 6:44 PM
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 service@frankbaxmann.de 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
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 <
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
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 <service@frankbaxmann.de> 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 <
>
> > service@
>
> > > 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
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
B
Baxi
Wed, Jun 13, 2018 8:51 PM
Yes Thx.
Works fine and the source looks much nicer.
--
Sent from: http://forum.openscad.org/
Yes Thx.
Works fine and the source looks much nicer.
--
Sent from: http://forum.openscad.org/