Author Topic: Maybe I'm just thick - Cond question...  (Read 2748 times)

0 Members and 1 Guest are viewing this topic.

CHulse

  • Swamp Rat
  • Posts: 504
Maybe I'm just thick - Cond question...
« on: July 27, 2023, 10:20:55 AM »
OK, maybe I'm just thick. Is one of these 2 snippets correct? Are they the same?  :uglystupid2:

Code - Auto/Visual Lisp: [Select]
  1.                                 (cond
  2.                                         ((not(or(equal "UPD Not Found" TXT1) (equal "" TXT1)))
  3.                                                 (setq TXT TXT1)
  4.                                         )
  5.                                        
  6.                                         ((not(or(equal "UPD Not Found" TXT2) (equal "" TXT2)))
  7.                                                 (setq TXT TXT2)
  8.                                         )
  9.                                                
  10.                                         ((not(equal "" FULL))
  11.                                                 (setq TXT FULL)
  12.                                         )
  13.                                                
  14.                                         ( T
  15.                                                 (setq TXT "NO TAG#")
  16.                                         )
  17.                                 );;_end cond
  18.  

Code - Auto/Visual Lisp: [Select]
  1.                         (setq TXT
  2.                                 (cond
  3.                                         ((not(or(equal "UPD Not Found" TXT1) (equal "" TXT1)))
  4.                                                 (TXT1)
  5.                                         )
  6.                                        
  7.                                         ((not(or(equal "UPD Not Found" TXT2) (equal "" TXT2)))
  8.                                                 (TXT2)
  9.                                         )
  10.                                                
  11.                                         ((not(equal "" FULL))
  12.                                                 (FULL)
  13.                                         )
  14.                                                
  15.                                         ( T
  16.                                                 ("NO TAG#")
  17.                                         )
  18.                                 );;_end cond   
  19.                         );;_end setq
  20.  
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Maybe I'm just thick - Cond question...
« Reply #1 on: July 27, 2023, 10:35:46 AM »
#2 is better. #1 reads like a second-grader's writing practice.

In essence, #2 reads like this: "Define the `TXT` variable and fill it with the results of the following conditional." #2 is a very common construct.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

d2010

  • Bull Frog
  • Posts: 326
Re: Maybe I'm just thick - Cond question...
« Reply #2 on: July 27, 2023, 10:58:54 AM »
If you have C++ experinced, then
--you replace
   (equal "UPD Not Found" TXT2)
--with
   (equal TXT2 "UPD Not Found").





CHulse

  • Swamp Rat
  • Posts: 504
Re: Maybe I'm just thick - Cond question...
« Reply #3 on: July 27, 2023, 12:40:52 PM »
Thanks guys
I've got a gremlin I can't ferret out and I thought it was this. I thought #2 was correct, but keep getting a nil return. I shall look elsewhere...

Why does the order of the elements matter after the "equal"?

Much appreciated.
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Maybe I'm just thick - Cond question...
« Reply #4 on: July 27, 2023, 01:02:07 PM »
The order of the elements matters in the higher order languages because of the "=" and "==" functions. One "tests" and the other "sets" equality.

It looks like your variable `TXT` will/should have at worse case contain "NO TAG#". Tracking down Booleans is difficult (first reason why #1 above is "incorrect" and #2 is "correct"). ...people love to toss around '(or (this) (that) (dosomething (somethingelse (setq...))))' around their code with very little thought about the returns of those statements.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CHulse

  • Swamp Rat
  • Posts: 504
Re: Maybe I'm just thick - Cond question...
« Reply #5 on: July 27, 2023, 01:54:05 PM »
Helpful, thank you. I'm struggling to get better at this and be able to do more than just stealing and tweaking other's programs.

I am still stumped by this though. The logic here seems sound to me, and I think the construct is correct, but it isn't assigning a value to TXT (always nil).

The watch shows me this:
TXT = nil
TAG2 = "598"
TAG1 = "UPD Not Found"
FULL = "t598"

So the second cond should return T correct?
Either way, TXT should get "NO TAG#" if nothing else, and it isn't.
I'm clearly missing something...

Code - Auto/Visual Lisp: [Select]
  1.                         (setq TXT
  2.                                 (cond
  3.                                         ((not(or(equal "UPD Not Found" TXT1) (equal "" TXT1)))
  4.                                                 TXT1
  5.                                         )
  6.                                        
  7.                                         ((not(or(equal "UPD Not Found" TXT2) (equal "" TXT2)))
  8.                                                 TXT2
  9.                                         )
  10.                                                
  11.                                         ((not (null FULL))
  12.                                                 FULL
  13.                                         )
  14.                                                
  15.                                         ( T
  16.                                                 "NO TAG#"
  17.                                         )
  18.                                 );;_end cond   
  19.                         );;_end setq
  20.  
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: Maybe I'm just thick - Cond question...
« Reply #6 on: July 27, 2023, 02:35:34 PM »
What--exactly--your statement is doing at a specific point in operation is a bit vague (we don't have enough information really to comment on which statement will be true) but this COND construct will not return TRUE/NIL it will return the value of the expression.

For example see if you can determine what var2 will be set to.
Code - Auto/Visual Lisp: [Select]
  1. (setq var1 1)
  2. (setq var2
  3.       (cond
  4.         ((eq var1 7) var1)
  5.         ((eq var1 6) "twenty one")
  6.         ((eq var1 1) 42)))

It looks to me as your variable `txt` should be set to `no tag#` (the last condition). What is the `txt` variable supposed to do?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2149
  • class keyThumper<T>:ILazy<T>
Re: Maybe I'm just thick - Cond question...
« Reply #7 on: July 27, 2023, 04:47:35 PM »
>> . .
--you replace
   (equal "UPD Not Found" TXT2)
--with
   (equal TXT2 "UPD Not Found").

It's not often I see the point of d2010 posts but in this case I agree.

'is the value of X "yes" '
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Jeff_M

  • King Gator
  • Posts: 4099
  • C3D user & customizer
Re: Maybe I'm just thick - Cond question...
« Reply #8 on: July 27, 2023, 05:09:55 PM »
Is it TAG1 & TAG2 or TXT1 & TXT2 ?  What your Watch says vs the code.

CHulse

  • Swamp Rat
  • Posts: 504
Re: Maybe I'm just thick - Cond question...
« Reply #9 on: July 27, 2023, 05:47:02 PM »
OMG. I have the variables named wrong.  :uglystupid2:
Any wonder it doesn’t work.
Thanks.
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Maybe I'm just thick - Cond question...
« Reply #10 on: July 28, 2023, 01:18:30 PM »
FWIW this:
Code - Auto/Visual Lisp: [Select]
  1. (or (equal "UPD Not Found" txt1) (equal "" txt1))
Could be this:
Code - Auto/Visual Lisp: [Select]
  1. (wcmatch txt1 "UPD Not Found,")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CHulse

  • Swamp Rat
  • Posts: 504
Re: Maybe I'm just thick - Cond question...
« Reply #11 on: July 28, 2023, 03:10:54 PM »
FWIW this:
Code - Auto/Visual Lisp: [Select]
  1. (or (equal "UPD Not Found" txt1) (equal "" txt1))
Could be this:
Code - Auto/Visual Lisp: [Select]
  1. (wcmatch txt1 "UPD Not Found,")

Interesting
Thanks Ron
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Maybe I'm just thick - Cond question...
« Reply #12 on: July 31, 2023, 03:31:20 PM »
FWIW this:
Code - Auto/Visual Lisp: [Select]
  1. (or (equal "UPD Not Found" txt1) (equal "" txt1))
Could be this:
Code - Auto/Visual Lisp: [Select]
  1. (wcmatch txt1 "UPD Not Found,")

Interesting
Thanks Ron
Anytime :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

BIGAL

  • Swamp Rat
  • Posts: 1429
  • 40 + years of using Autocad
Re: Maybe I'm just thick - Cond question...
« Reply #13 on: July 31, 2023, 06:59:20 PM »
For me a simpler way to read the cond

Code: [Select]
     (cond
             ((wcmatch txt1 "UPD Not Found")(setq txt TXT1))
             ((wcmatch txt2 "UPD Not Found")(setq txt TXT2))
             ((null FULL)(setq txt full))
             ((setq txt "NO TAG#"))
     );;_end cond
« Last Edit: August 01, 2023, 08:47:54 PM by BIGAL »
A man who never made a mistake never made anything

bruno_vdh

  • Newt
  • Posts: 107
Re: Maybe I'm just thick - Cond question...
« Reply #14 on: August 01, 2023, 03:32:35 AM »
Hello,
FWIW this:
Code - Auto/Visual Lisp: [Select]
  1. (not(or(equal "UPD Not Found" TXT1) (equal "" TXT1)))
Could be this:
Code - Auto/Visual Lisp: [Select]
  1. (/= "UPD Not Found" txt1 "")

bruno_vdh

  • Newt
  • Posts: 107
Re: Maybe I'm just thick - Cond question...
« Reply #15 on: August 01, 2023, 03:52:45 AM »
OK, maybe I'm just thick. Is one of these 2 snippets correct? Are they the same?  :uglystupid2:
#1 can be written with or, while #2 can only be constructed with cond, if that helps your thinking...

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Maybe I'm just thick - Cond question...
« Reply #16 on: August 01, 2023, 11:38:44 AM »
For me a simpler way to read the cond

Code: [Select]
     (cond
             ((wcmatch txt1 "UPD Not Found"))(setq txt TXT1))
             ((wcmatch txt2 "UPD Not Found"))(setq txt TXT2))
             ((null FULL))(setq txt full)
             ((setq txt "NO TAG#"))
     );;_end cond
"Unbalanced closing brackets."

Also .. you can pull the (setq txt outside of the COND and only return the values.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

BIGAL

  • Swamp Rat
  • Posts: 1429
  • 40 + years of using Autocad
Re: Maybe I'm just thick - Cond question...
« Reply #17 on: August 01, 2023, 08:47:07 PM »
Thanks ronjonp just typed did not check

Code: [Select]
(cond
             ((wcmatch txt1 "UPD Not Found")(setq txt TXT1))
             ((wcmatch txt2 "UPD Not Found")(setq txt TXT2))
             ((null FULL)(setq txt full))
             ((setq txt "NO TAG#"))
     );;_end cond
A man who never made a mistake never made anything

steve.carson

  • Newt
  • Posts: 108
Re: Maybe I'm just thick - Cond question...
« Reply #18 on: August 02, 2023, 10:58:22 AM »
I know when I first started using cond, I always got tripped up by the parenthesis so I would type something like this first, just to keep things straight in my head:

Code - Auto/Visual Lisp: [Select]
  1.     ((         ) ;<-- First test
  2.         (          ) ;<-- Do this
  3.         (          ) ;<-- And this
  4.     )
  5.     ((         ) ;<-- Second test
  6.         (          ) ;<-- Do this
  7.         (          ) ;<-- And this
  8.     )
  9.     ((         ) ;<-- Third test
  10.         (          ) ;<-- Do this
  11.         (          ) ;<-- And this
  12.     )
  13. ); cond
  14.  

And then go back and start filling it in with what I want it to do.

CHulse

  • Swamp Rat
  • Posts: 504
Re: Maybe I'm just thick - Cond question...
« Reply #19 on: August 02, 2023, 11:53:21 AM »
This is what I eventually ended up with (and is working nicely).
Thanks to everyone for the help with this.

Code - Auto/Visual Lisp: [Select]
  1.                         (setq TXT
  2.                                 (cond
  3.                                         ((not(or (wcmatch TAG1 "UPD Not Found,0,")(null TAG1))) ;;check if UDP not found, empty string, 0, or null
  4.                                                 TAG1
  5.                                         )
  6.                                         ((not(or(wcmatch TAG2 "UPD Not Found,0,")(null TAG2)))
  7.                                                 TAG2
  8.                                         )
  9.                                         ((not(or(wcmatch FULL "0,")(null FULL) ))  
  10.                                                 FULL
  11.                                         )      
  12.                                         ( T
  13.                                                 "NO TAG#"
  14.                                         )
  15.                                 );;_end cond   
  16.                         );;_end setq
  17.  
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

bruno_vdh

  • Newt
  • Posts: 107
Re: Maybe I'm just thick - Cond question...
« Reply #20 on: August 03, 2023, 03:55:19 AM »
Hi,
This is what I eventually ended up with (and is working nicely).

Just a small note, it is better to check if the variable is null first:
Code: [Select]
_$ (setq TAG1 nil)
nil
_$ (not (or (wcmatch TAG1 "UPD Not Found,0,") (null TAG1)))
; erreur: type d'argument incorrect: stringp nil
_$ (not (or (null TAG1) (wcmatch TAG1 "UPD Not Found,0,")))
nil

bruno_vdh

  • Newt
  • Posts: 107
Re: Maybe I'm just thick - Cond question...
« Reply #21 on: August 03, 2023, 05:29:19 AM »
Just a small note, it is better to check if the variable is null first:
Code: [Select]
_$ (setq TAG1 nil)
nil
_$ (not (or (wcmatch TAG1 "UPD Not Found,0,") (null TAG1)))
; erreur: type d'argument incorrect: stringp nil
_$ (not (or (null TAG1) (wcmatch TAG1 "UPD Not Found,0,")))
nil

To avoid type errors, you can also use the member function as a predicate
Code - Auto/Visual Lisp: [Select]
  1. (not (member TAG1 '("UPD Not Found" "0" "" nil)))