### Variables set within an If/else statement are not known outside of it?

JW
Joe Weinpert
Sat, Jan 15, 2022 10:08 PM

What am I missing here?

I need to set a new array of points with the last index removed from the
original array or the whole original array depending on if the last one in
the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF statement?

What am I missing here? I need to set a new array of points with the last index removed from the original array or the whole original array depending on if the last one in the original array equals the first one in the original array. Essentially: x = len( objPoints )-1; if( str( objPoints[x] ) == str( objPoints[0] ) ){ newPoints = list_remove( objPoints, x ); }else{ newPoints = objPoints; } echo( newPoints ); So why isn't the newPoints variable recognized outside the IF statement?
NH
Sat, Jan 15, 2022 10:16 PM

Because variables only have scope to the end of the block they are defined
in. I.e. to the closing braces.

You need to use the ? operator to give a variable a conditional value.

newpoints = str( objPoints[x] ) == str( objPoints[0]  ? list_remove(
objPoints, x ) : objPoints;

On Sat, 15 Jan 2022 at 22:09, Joe Weinpert joe.weinpert@gmail.com wrote:

What am I missing here?

I need to set a new array of points with the last index removed from the
original array or the whole original array depending on if the last one in
the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

Because variables only have scope to the end of the block they are defined in. I.e. to the closing braces. You need to use the ? operator to give a variable a conditional value. newpoints = str( objPoints[x] ) == str( objPoints[0] ? list_remove( objPoints, x ) : objPoints; On Sat, 15 Jan 2022 at 22:09, Joe Weinpert <joe.weinpert@gmail.com> wrote: > What am I missing here? > > I need to set a new array of points with the last index removed from the > original array or the whole original array depending on if the last one in > the original array equals the first one in the original array. > > Essentially: > > x = len( objPoints )-1; > if( str( objPoints[x] ) == str( objPoints[0] ) ){ > newPoints = list_remove( objPoints, x ); > }else{ > newPoints = objPoints; > } > echo( newPoints ); > > > So why isn't the newPoints variable recognized outside the IF statement? > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
AM
Sat, Jan 15, 2022 10:22 PM

Variables can only be set once.  If you think you're setting one a
second time it's actually in a nested scope and it will vanish once
you leave the scope.  With let() statements you can cascade scopes in
a way that looks like you are redefining the variables, but each let
opens a new scope.

Looks like nophead has explained a way around this using the ?
operator.  You might also want to use BOSL2's cleanup_path() function,

On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert joe.weinpert@gmail.com wrote:

What am I missing here?

I need to set a new array of points with the last index removed from the original array or the whole original array depending on if the last one in the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

Variables can only be set once. If you think you're setting one a second time it's actually in a nested scope and it will vanish once you leave the scope. With let() statements you can cascade scopes in a way that looks like you are redefining the variables, but each let opens a new scope. Looks like nophead has explained a way around this using the ? operator. You might also want to use BOSL2's cleanup_path() function, assuming your data is numeric. On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert <joe.weinpert@gmail.com> wrote: > > What am I missing here? > > I need to set a new array of points with the last index removed from the original array or the whole original array depending on if the last one in the original array equals the first one in the original array. > > Essentially: > > x = len( objPoints )-1; > if( str( objPoints[x] ) == str( objPoints[0] ) ){ > newPoints = list_remove( objPoints, x ); > }else{ > newPoints = objPoints; > } > echo( newPoints ); > > > So why isn't the newPoints variable recognized outside the IF statement? > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
JW
Joe Weinpert
Sat, Jan 15, 2022 10:34 PM

Thanks nop. I will use the inline ifs

Because variables only have scope to the end of the block they are defined
in. I.e. to the closing braces.

You need to use the ? operator to give a variable a conditional value.

newpoints = str( objPoints[x] ) == str( objPoints[0]  ? list_remove(
objPoints, x ) : objPoints;

On Sat, 15 Jan 2022 at 22:09, Joe Weinpert joe.weinpert@gmail.com wrote:

What am I missing here?

I need to set a new array of points with the last index removed from the
original array or the whole original array depending on if the last one in
the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

Thanks nop. I will use the inline ifs On Sat, Jan 15, 2022, 5:16 PM nop head <nop.head@gmail.com> wrote: > Because variables only have scope to the end of the block they are defined > in. I.e. to the closing braces. > > You need to use the ? operator to give a variable a conditional value. > > newpoints = str( objPoints[x] ) == str( objPoints[0] ? list_remove( > objPoints, x ) : objPoints; > > > > On Sat, 15 Jan 2022 at 22:09, Joe Weinpert <joe.weinpert@gmail.com> wrote: > >> What am I missing here? >> >> I need to set a new array of points with the last index removed from the >> original array or the whole original array depending on if the last one in >> the original array equals the first one in the original array. >> >> Essentially: >> >> x = len( objPoints )-1; >> if( str( objPoints[x] ) == str( objPoints[0] ) ){ >> newPoints = list_remove( objPoints, x ); >> }else{ >> newPoints = objPoints; >> } >> echo( newPoints ); >> >> >> So why isn't the newPoints variable recognized outside the IF statement? >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JW
Joe Weinpert
Sat, Jan 15, 2022 10:39 PM

I am finding answers here, so it is appreciated.  Will look deeper into the
BOSL library as I go.  Knowing what to look for is tough.

On Sat, Jan 15, 2022, 5:22 PM Adrian Mariano avm4@cornell.edu wrote:

Variables can only be set once.  If you think you're setting one a
second time it's actually in a nested scope and it will vanish once
you leave the scope.  With let() statements you can cascade scopes in
a way that looks like you are redefining the variables, but each let
opens a new scope.

Looks like nophead has explained a way around this using the ?
operator.  You might also want to use BOSL2's cleanup_path() function,

On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert joe.weinpert@gmail.com
wrote:

What am I missing here?

I need to set a new array of points with the last index removed from the

original array or the whole original array depending on if the last one in
the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

I am finding answers here, so it is appreciated. Will look deeper into the BOSL library as I go. Knowing what to look for is tough. On Sat, Jan 15, 2022, 5:22 PM Adrian Mariano <avm4@cornell.edu> wrote: > Variables can only be set once. If you think you're setting one a > second time it's actually in a nested scope and it will vanish once > you leave the scope. With let() statements you can cascade scopes in > a way that looks like you are redefining the variables, but each let > opens a new scope. > > Looks like nophead has explained a way around this using the ? > operator. You might also want to use BOSL2's cleanup_path() function, > assuming your data is numeric. > > On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert <joe.weinpert@gmail.com> > wrote: > > > > What am I missing here? > > > > I need to set a new array of points with the last index removed from the > original array or the whole original array depending on if the last one in > the original array equals the first one in the original array. > > > > Essentially: > > > > x = len( objPoints )-1; > > if( str( objPoints[x] ) == str( objPoints[0] ) ){ > > newPoints = list_remove( objPoints, x ); > > }else{ > > newPoints = objPoints; > > } > > echo( newPoints ); > > > > > > So why isn't the newPoints variable recognized outside the IF statement? > > > > _______________________________________________ > > OpenSCAD mailing list > > To unsubscribe send an email to discuss-leave@lists.openscad.org > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Sat, Jan 15, 2022 11:02 PM

Just to play with alternative implementations...

You have a list of points, and if the first point is the same as the
last point then you want a new list that omits that last point, right?
Here's another way to approach it.

``````// Given a list a...
// Decide how many elements of a we are going to preserve.
n = a[0] == a[len(a)-1] ? len(a)-1 : len(a);
// Generate that resulting list.
a2 = [ for (i=[0:1:n-1]) a[i] ];
``````

That works, but I don't like that it accesses out of bounds on an empty
list (even though it correctly produces an empty list), and presumably
it's wrong to turn a one-entry list into an empty list (even though the
first and last elements are the same).  Thus I'd add another check:

``````// Given a list a...
// Decide how many elements of a we are going to preserve.
n = (len(a) > 1 && a[0] == a[len(a)-1]) ? len(a)-1 : len(a);
// Generate that resulting list.
a2 = [ for (i=[0:1:n-1]) a[i] ];
``````

or, wrapping that up into a function:

``````function clean(a) =
let(n = (len(a) > 1 && a[0] == a[len(a)-1]) ? len(a)-1 : len(a))
[ for (i=[0:1:n-1]) a[i] ];
``````
Just to play with alternative implementations... You have a list of points, and if the first point is the same as the last point then you want a new list that omits that last point, right?  Here's another way to approach it. // Given a list a... // Decide how many elements of a we are going to preserve. n = a[0] == a[len(a)-1] ? len(a)-1 : len(a); // Generate that resulting list. a2 = [ for (i=[0:1:n-1]) a[i] ]; That works, but I don't like that it accesses out of bounds on an empty list (even though it correctly produces an empty list), and presumably it's wrong to turn a one-entry list into an empty list (even though the first and last elements *are* the same).  Thus I'd add another check: // Given a list a... // Decide how many elements of a we are going to preserve. n = (len(a) > 1 && a[0] == a[len(a)-1]) ? len(a)-1 : len(a); // Generate that resulting list. a2 = [ for (i=[0:1:n-1]) a[i] ]; or, wrapping that up into a function: function clean(a) = let(n = (len(a) > 1 && a[0] == a[len(a)-1]) ? len(a)-1 : len(a)) [ for (i=[0:1:n-1]) a[i] ];
AM
Sat, Jan 15, 2022 11:26 PM

It occurs to me that if you're running deduplicate already when
deduplicate, and it will remove repeated points between the start and
end as well.  The BOSL2 deduplicate implementation considers points
equal if they are closer than eps, which defaults to 1e-9.  I don't
think doing exact compares for floats is the best plan, though I guess
it might work.  (You could end up with some very tiny triangles in

On Sat, Jan 15, 2022 at 5:40 PM Joe Weinpert joe.weinpert@gmail.com wrote:

I am finding answers here, so it is appreciated.  Will look deeper into the BOSL library as I go.  Knowing what to look for is tough.

On Sat, Jan 15, 2022, 5:22 PM Adrian Mariano avm4@cornell.edu wrote:

Variables can only be set once.  If you think you're setting one a
second time it's actually in a nested scope and it will vanish once
you leave the scope.  With let() statements you can cascade scopes in
a way that looks like you are redefining the variables, but each let
opens a new scope.

Looks like nophead has explained a way around this using the ?
operator.  You might also want to use BOSL2's cleanup_path() function,

On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert joe.weinpert@gmail.com wrote:

What am I missing here?

I need to set a new array of points with the last index removed from the original array or the whole original array depending on if the last one in the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

JW
Joe Weinpert
Sat, Jan 15, 2022 11:46 PM

I will try that, too.  However, The description of the "Closed" clause I
read as being only based on the value of each value within the sub arrays
being checked ... i.e. check only the X value and if it matches delete the
row without needing to check the Y value.

On Sat, Jan 15, 2022, 6:27 PM Adrian Mariano avm4@cornell.edu wrote:

It occurs to me that if you're running deduplicate already when
deduplicate, and it will remove repeated points between the start and
end as well.  The BOSL2 deduplicate implementation considers points
equal if they are closer than eps, which defaults to 1e-9.  I don't
think doing exact compares for floats is the best plan, though I guess
it might work.  (You could end up with some very tiny triangles in

On Sat, Jan 15, 2022 at 5:40 PM Joe Weinpert joe.weinpert@gmail.com
wrote:

I am finding answers here, so it is appreciated.  Will look deeper into

the BOSL library as I go.  Knowing what to look for is tough.

On Sat, Jan 15, 2022, 5:22 PM Adrian Mariano avm4@cornell.edu wrote:

Variables can only be set once.  If you think you're setting one a
second time it's actually in a nested scope and it will vanish once
you leave the scope.  With let() statements you can cascade scopes in
a way that looks like you are redefining the variables, but each let
opens a new scope.

Looks like nophead has explained a way around this using the ?
operator.  You might also want to use BOSL2's cleanup_path() function,

On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert joe.weinpert@gmail.com

wrote:

What am I missing here?

I need to set a new array of points with the last index removed from

the original array or the whole original array depending on if the last one
in the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF

statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

AM
Sun, Jan 16, 2022 12:10 AM

Can you elaborate on what you are referring to (something in the BOSL2
docs?) that made you think that "equality" referred only the x
coordinate?    How could the docs be clarified to improve this?

On Sat, Jan 15, 2022 at 6:46 PM Joe Weinpert joe.weinpert@gmail.com wrote:

I will try that, too.  However, The description of the "Closed" clause I read as being only based on the value of each value within the sub arrays  being checked ... i.e. check only the X value and if it matches delete the row without needing to check the Y value.

On Sat, Jan 15, 2022, 6:27 PM Adrian Mariano avm4@cornell.edu wrote:

It occurs to me that if you're running deduplicate already when
deduplicate, and it will remove repeated points between the start and
end as well.  The BOSL2 deduplicate implementation considers points
equal if they are closer than eps, which defaults to 1e-9.  I don't
think doing exact compares for floats is the best plan, though I guess
it might work.  (You could end up with some very tiny triangles in

On Sat, Jan 15, 2022 at 5:40 PM Joe Weinpert joe.weinpert@gmail.com wrote:

I am finding answers here, so it is appreciated.  Will look deeper into the BOSL library as I go.  Knowing what to look for is tough.

On Sat, Jan 15, 2022, 5:22 PM Adrian Mariano avm4@cornell.edu wrote:

Variables can only be set once.  If you think you're setting one a
second time it's actually in a nested scope and it will vanish once
you leave the scope.  With let() statements you can cascade scopes in
a way that looks like you are redefining the variables, but each let
opens a new scope.

Looks like nophead has explained a way around this using the ?
operator.  You might also want to use BOSL2's cleanup_path() function,

On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert joe.weinpert@gmail.com wrote:

What am I missing here?

I need to set a new array of points with the last index removed from the original array or the whole original array depending on if the last one in the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

JW
Joe Weinpert
Sun, Jan 16, 2022 4:07 PM

Well, to me I am looking at a list as an array.  Was not sure what "closed"
meant.  I am guessing now that it is something in the world of mesh. My
ignorance of the OpenSCAD terminology will stay with me for a long time.  I
was expecting something like javaScript (not sure why).  If I understand,
it now means to remove any and all end rows in the array that match the
very first row.

Joe Weinpert
(440) 796-7165
joe.weinpert@gmail.com

On Sat, Jan 15, 2022 at 7:11 PM Adrian Mariano avm4@cornell.edu wrote:

Can you elaborate on what you are referring to (something in the BOSL2
docs?) that made you think that "equality" referred only the x
coordinate?    How could the docs be clarified to improve this?

On Sat, Jan 15, 2022 at 6:46 PM Joe Weinpert joe.weinpert@gmail.com
wrote:

I will try that, too.  However, The description of the "Closed" clause I

read as being only based on the value of each value within the sub arrays
being checked ... i.e. check only the X value and if it matches delete the
row without needing to check the Y value.

On Sat, Jan 15, 2022, 6:27 PM Adrian Mariano avm4@cornell.edu wrote:

It occurs to me that if you're running deduplicate already when
deduplicate, and it will remove repeated points between the start and
end as well.  The BOSL2 deduplicate implementation considers points
equal if they are closer than eps, which defaults to 1e-9.  I don't
think doing exact compares for floats is the best plan, though I guess
it might work.  (You could end up with some very tiny triangles in

On Sat, Jan 15, 2022 at 5:40 PM Joe Weinpert joe.weinpert@gmail.com

wrote:

I am finding answers here, so it is appreciated.  Will look deeper

into the BOSL library as I go.  Knowing what to look for is tough.

On Sat, Jan 15, 2022, 5:22 PM Adrian Mariano avm4@cornell.edu

wrote:

Variables can only be set once.  If you think you're setting one a
second time it's actually in a nested scope and it will vanish once
you leave the scope.  With let() statements you can cascade scopes in
a way that looks like you are redefining the variables, but each let
opens a new scope.

Looks like nophead has explained a way around this using the ?
operator.  You might also want to use BOSL2's cleanup_path()

function,

On Sat, Jan 15, 2022 at 5:09 PM Joe Weinpert joe.weinpert@gmail.com

wrote:

What am I missing here?

I need to set a new array of points with the last index removed

from the original array or the whole original array depending on if the
last one in the original array equals the first one in the original array.

Essentially:

x = len( objPoints )-1;
if( str( objPoints[x] ) == str( objPoints[0] ) ){
newPoints = list_remove( objPoints, x );
}else{
newPoints = objPoints;
}
echo( newPoints );

So why isn't the newPoints variable recognized outside the IF

statement?

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org