I have a logic problem

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

I have a logic problem

dpa
Hi,
I want to deside what to show/render and I used a string variable to have clear description. And I have "both" as part of the variant what should something more. So every object should be visible with its own name and "both". I decided to use logical OR. But the comparison makes an error I don't know yet.

Let me give you this example. I use only view="body" and true/false tool to show the logic.

// scad

view = "body";
echo(view_check =  view);

view_check1 = (view == "bla" || "both") ? true : false ;
echo (view_check1 = view_check1);
// this echo should say false because both strings are wrong

view_check2 = (view == ("body" || "both")) ? true : false ;
echo (view_check2 = view_check2);
// this echo should be true because view = "body" is true

// end of scad
 

The result of echos is confusing:

ECHO: view_check = "body"

ECHO: view_check1 = true

ECHO: view_check2 = false


The first echo is just to see the correct input.
The second one should be false because no "bla" and no "both" is there, but it is true ?
The third echo should be true because "body" is one true option, but it is false ?

The difference between view_check1 and 2 is, that I added a bracket in order to get rid of the error but it made the confusion worse..

Can you explain me what I'm doing wrong and how to do the comparison correctly?


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

Re: I have a logic problem

Tim V. Shaporev
Try view_check1 = view == "bla" || view == "both";

P.S. "? true : false" is really bad style.

On 6/4/2020 7:12 PM, dpa wrote:

> Hi,
> I want to deside what to show/render and I used a string variable to
> have clear description. And I have "both" as part of the variant
> what should something more. So every object should be visible with its
> own name and "both". I decided to use logical OR. But the comparison
> makes an error I don't know yet.
>
> Let me give you this example. I use only view="body" and true/false tool
> to show the logic.
>
> // scad
>
> view = "body";
> echo(view_check =  view);
>
> view_check1 = (view == "bla" || "both") ? true : false ;
> echo (view_check1 = view_check1);
> // this echo should say false because both strings are wrong
>
> view_check2 = (view == ("body" || "both")) ? true : false ;
> echo (view_check2 = view_check2);
> // this echo should be true because view = "body" is true
>
> // end of scad
>
>
> The result of echos is confusing:
>
> ECHO: view_check = "body"
>
> ECHO: view_check1 = true
>
> ECHO: view_check2 = false
>
>
> The first echo is just to see the correct input.
> The second one should be *false* because no "bla" and no "both" is
> there, *but it is true ?*
> The third echo should be *true* because "body" is one true option, *but
> it is false ?*
>
> The difference between view_check1 and 2 is, that I added a bracket in
> order to get rid of the error but it made the confusion worse..
>
> Can you explain me what I'm doing wrong and how to do the comparison
> correctly?
>
>
> _______________________________________________
> 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: I have a logic problem

nophead
In reply to this post by dpa
You just need  

view_check1 = view == "bla" || view =="both";

No need to test to make true and false because == returns true or false and you must compare view twice. 

On Thu, 4 Jun 2020 at 17:12, dpa <[hidden email]> wrote:
Hi,
I want to deside what to show/render and I used a string variable to have clear description. And I have "both" as part of the variant what should something more. So every object should be visible with its own name and "both". I decided to use logical OR. But the comparison makes an error I don't know yet.

Let me give you this example. I use only view="body" and true/false tool to show the logic.

// scad

view = "body";
echo(view_check =  view);

view_check1 = (view == "bla" || "both") ? true : false ;
echo (view_check1 = view_check1);
// this echo should say false because both strings are wrong

view_check2 = (view == ("body" || "both")) ? true : false ;
echo (view_check2 = view_check2);
// this echo should be true because view = "body" is true

// end of scad
 

The result of echos is confusing:

ECHO: view_check = "body"

ECHO: view_check1 = true

ECHO: view_check2 = false


The first echo is just to see the correct input.
The second one should be false because no "bla" and no "both" is there, but it is true ?
The third echo should be true because "body" is one true option, but it is false ?

The difference between view_check1 and 2 is, that I added a bracket in order to get rid of the error but it made the confusion worse..

Can you explain me what I'm doing wrong and how to do the comparison correctly?

_______________________________________________
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: I have a logic problem

MostlyHarmless
In reply to this post by dpa
Try

view_check2 = view == "body" || view == "both";


Regards, Jan


On Thu, Jun 4, 2020 at 12:13 PM dpa <[hidden email]> wrote:
Hi,
I want to deside what to show/render and I used a string variable to have clear description. And I have "both" as part of the variant what should something more. So every object should be visible with its own name and "both". I decided to use logical OR. But the comparison makes an error I don't know yet.

Let me give you this example. I use only view="body" and true/false tool to show the logic.

// scad

view = "body";
echo(view_check =  view);

view_check1 = (view == "bla" || "both") ? true : false ;
echo (view_check1 = view_check1);
// this echo should say false because both strings are wrong

view_check2 = (view == ("body" || "both")) ? true : false ;
echo (view_check2 = view_check2);
// this echo should be true because view = "body" is true

// end of scad
 

The result of echos is confusing:

ECHO: view_check = "body"

ECHO: view_check1 = true

ECHO: view_check2 = false


The first echo is just to see the correct input.
The second one should be false because no "bla" and no "both" is there, but it is true ?
The third echo should be true because "body" is one true option, but it is false ?

The difference between view_check1 and 2 is, that I added a bracket in order to get rid of the error but it made the confusion worse..

Can you explain me what I'm doing wrong and how to do the comparison correctly?

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


--
Jan Wieck
Principal Database Engineer

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

Re: I have a logic problem

dpa
thank you!

Am Do., 4. Juni 2020 um 18:31 Uhr schrieb Jan Wieck <[hidden email]>:
Try

view_check2 = view == "body" || view == "both";


Regards, Jan


On Thu, Jun 4, 2020 at 12:13 PM dpa <[hidden email]> wrote:
Hi,
I want to deside what to show/render and I used a string variable to have clear description. And I have "both" as part of the variant what should something more. So every object should be visible with its own name and "both". I decided to use logical OR. But the comparison makes an error I don't know yet.

Let me give you this example. I use only view="body" and true/false tool to show the logic.

// scad

view = "body";
echo(view_check =  view);

view_check1 = (view == "bla" || "both") ? true : false ;
echo (view_check1 = view_check1);
// this echo should say false because both strings are wrong

view_check2 = (view == ("body" || "both")) ? true : false ;
echo (view_check2 = view_check2);
// this echo should be true because view = "body" is true

// end of scad
 

The result of echos is confusing:

ECHO: view_check = "body"

ECHO: view_check1 = true

ECHO: view_check2 = false


The first echo is just to see the correct input.
The second one should be false because no "bla" and no "both" is there, but it is true ?
The third echo should be true because "body" is one true option, but it is false ?

The difference between view_check1 and 2 is, that I added a bracket in order to get rid of the error but it made the confusion worse..

Can you explain me what I'm doing wrong and how to do the comparison correctly?

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


--
Jan Wieck
Principal Database Engineer
_______________________________________________
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