# TheSwamp

## Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CHulse on November 10, 2010, 10:27:54 AM

Title: newbie question: COND limits?
Post by: CHulse on November 10, 2010, 10:27:54 AM
I'm sure this is a newbie question, but is there a max limit to how many conditions can be tested with a COND statement?

Thanks
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 10, 2010, 11:12:23 AM
I think its 256, don't quote me on that though...
Title: Re: newbie question: COND limits?
Post by: mjfarrell on November 10, 2010, 11:13:39 AM
I think its 256...

;-)
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 10, 2010, 11:15:23 AM
:-D
Title: Re: newbie question: COND limits?
Post by: CHulse on November 10, 2010, 11:27:38 AM
Thanks
Title: Re: newbie question: COND limits?
Post by: CAB on November 10, 2010, 12:29:37 PM
Sounds like you need some refining of your algorithm if you have anywhere near that many comparisons.
Care to share your code problem? 8-)
Title: Re: newbie question: COND limits?
Post by: jbuzbee on November 10, 2010, 12:39:17 PM
He's attempting to write an algorithm to predict women . . ..

OK before any of you chicks start throwing stones my wife and I have been happily married for 26 years - that makes me an expert on the fact you can NEVER be figured out!  ^-^
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 10, 2010, 12:58:29 PM
Sounds like you need some refining of your algorithm if you have anywhere near that many comparisons.
Care to share your code problem? 8-)

x2
Title: Re: newbie question: COND limits?
Post by: Jeff_M on November 10, 2010, 03:30:19 PM
AFAIK, there is no limit to the number of tests in a condition. From the lisp docs:
The cond function accepts any number of lists as arguments.

Not that I'd want to use a large number of tests....there're usually better ways to attack the problem.
Title: Re: newbie question: COND limits?
Post by: CHulse on November 10, 2010, 04:49:03 PM
Well, being a lisp noob, I work with what I know.  :wink:
At this point I don't know exactly what I will be doing. Let me explain :)
I want to insert blocks from a CSV file (thanks Lee Mac for that original code). Normally I only have a few attribs to add to the block. I was approached with a new project where I may have many attribs to apply. The code I have uses a cond to match the attrib tag to the coorect variable. I just wasn't sure if there was a limit to that process. I'm not sure yet how may attribs each block will have on this, but I expect it to be 10 or more.

this is the method I have to apply the data to the block attribs:

Code: [Select]
`(foreach att (vlax-invoke obj 'GetAttributes)                (COND ((EQ "ID" (VLA-GET-TAGSTRING att))                        (VLA-PUT-TEXTSTRING att tnum)                       )                       ((EQ "BOTANICAL" (VLA-GET-TAGSTRING att))                        (VLA-PUT-TEXTSTRING att species)                       )                 ) ;_ end of cond         );_ end of foreach`

Any insight as to how I could do this better would be appreciated, just use small words to explain it and speak slowly :)
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 10, 2010, 04:53:26 PM
Perhaps use an association list:

Code: [Select]
`((<tag> . <value>) (<tag> . <value>) ... (<tag> . <value>))`
Then use assoc to obtain the correct value:

Code: [Select]
`(if (setq val (cdr (assoc (vla-get-TagString <att>) <alist>)))  (vla-put-TextString <att> val))`
Title: Re: newbie question: COND limits?
Post by: CHulse on November 10, 2010, 04:57:59 PM
Sweet, I'll try that. That seems simple enough and would clean up the code for that many atts...
Thanks!!
Title: Re: newbie question: COND limits?
Post by: alanjt on November 10, 2010, 05:04:22 PM
FYI, assoc doesn't require a dotted pair, it just searched the first item in each list.

Also, assoc is case-sensitive, so be sure capitalize the search item in the defined list and (strcase (vla-get-TagString <att>)) before trying to search with assoc.
Title: Re: newbie question: COND limits?
Post by: CHulse on November 10, 2010, 05:17:21 PM
FYI, assoc doesn't require a dotted pair, it just searched the first item in each list.

Also, assoc is case-sensitive, so be sure capitalize the search item in the defined list and (strcase (vla-get-TagString <att>)) before trying to search with assoc.

Would COND not require those steps?
Title: Re: newbie question: COND limits?
Post by: alanjt on November 10, 2010, 05:21:42 PM
FYI, assoc doesn't require a dotted pair, it just searched the first item in each list.

Also, assoc is case-sensitive, so be sure capitalize the search item in the defined list and (strcase (vla-get-TagString <att>)) before trying to search with assoc.

Would COND not require those steps?
both ways still have to compare with tagstring.
Title: Re: newbie question: COND limits?
Post by: CHulse on November 10, 2010, 05:30:46 PM
FYI, assoc doesn't require a dotted pair, it just searched the first item in each list.

Also, assoc is case-sensitive, so be sure capitalize the search item in the defined list and (strcase (vla-get-TagString <att>)) before trying to search with assoc.

Would COND not require those steps?
both ways still have to compare with tagstring.
I meant the case sensative question - is cond NOT case sensative?

It took me a minute to get what you were doing, but that makes sense. Thanks
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 10, 2010, 05:55:01 PM
I meant the case sensative question - is cond NOT case sensative?

Try it:

Code: [Select]
`(eq "ab" "AB")`
Then you tell me  :wink:
Title: Re: newbie question: COND limits?
Post by: ronjonp on November 10, 2010, 06:04:08 PM
FYI, assoc doesn't require a dotted pair, it just searched the first item in each list.

Also, assoc is case-sensitive, so be sure capitalize the search item in the defined list and (strcase (vla-get-TagString <att>)) before trying to search with assoc.

I don't think you need the strcase on the vla-get-TagString. I thought the tagstring was always uppercase?
Title: Re: newbie question: COND limits?
Post by: CHulse on November 10, 2010, 06:09:14 PM
I meant the case sensative question - is cond NOT case sensative?

Try it:

Code: [Select]
`(eq "ab" "AB")`
Then you tell me  :wink:

HaHa, point taken...
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 04:49:41 PM
Ok, I couldn't get the ASSOC concept to work for me and I'm not sure what I was doing wrong...

Code: [Select]
`(setq atlist '((ID tnum)(BOTANICAL species)(COMMON common)(CULTIVAR cultivar)(DBH dbh)(CONDITION condition)(UNIQUEID unique)(STEMS stem)))(foreach att (vlax-invoke obj 'GetAttributes)         (if (setq attval (cdr (assoc (strcase(vla-get-TagString att)) atlist)))             (vla-put-TextString att attval)))`
Any idea what I did wrong here?
Thanks
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 04:58:04 PM
Two problems:

1) The use of the apostrophe: see if you can work out why after reading this:

2) The use of 'cdr' without use a dotted pair, read the help docs on 'cdr' and see if you can work out a fix.

:-)
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 05:22:45 PM
Ok, so I should have used CADR in this situation then correct?

As for the ' use, I took that from the help file example for ASSOC:

Code: [Select]
`ExamplesCommand: (setq al '((name box) (width 3) (size 4.7263) (depth 5)))`
I assumed you would need to identify the alist as a "list"...  is that non needed?
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 05:28:53 PM
Ok, so I should have used CADR in this situation then correct?

Correct for the list structure in your code, else you could use a dotted list with cdr  :-)

As for the ' use, I took that from the help file example for ASSOC...

Did you read my link regarding the apostrophe? The problem is in what elements are evaluated  :-)
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 05:51:14 PM
I did, but I'm not sure I get it...
Since I have variables in my list, does the ' mean the variables are not evaluated? The example is only values, not vars...?
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 05:59:57 PM
Since I have variables in my list, does the ' mean the variables are not evaluated?

Exactly  :-)

Also, spotted one more thing - tagstrings

Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 06:25:24 PM
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 07:00:02 PM

Well, in your current code they aren't strings - hence my hint  :lol:
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 07:09:29 PM
Ooohh - I assume you mean they need to be "quoted"??
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 07:10:13 PM
Thanks for your help (again :))
It's late, bear with me here...
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 07:19:03 PM
Ooohh - I assume you mean they need to be "quoted"??

Yes, exactly  :-)

Thanks for your help (again :))
It's late, bear with me here...

You're welcome, and don't worry - I thought it'd be better to provide hints than to just give the complete code  :-)  But maybe some of my hints aren't too clear :-(
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 07:24:04 PM
I thought it'd be better to provide hints than to just give the complete code  :-)  But maybe some of my hints aren't too clear :-(

Believe me, I appreciate it. Hints are best (usually) as it actually helps me learn :)
The clarity issue is that I have been up since about 3am  :ugly: , not your hints
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 07:25:00 PM
Ok, but now I get this:

** Error: bad function: "BOTANICAL" **

Code: [Select]
`(setq atlist (("ID" tnum)("BOTANICAL" species)("COMMON" common)("CULTIVAR" cultivar)("DBH" dbh)("CONDITION" condition)("UNIQUEID" unique)("STEMS" stem)))(foreach att (vlax-invoke obj 'GetAttributes)            (if (setq attval (cadr (assoc (strcase(vla-get-TagString att)) atlist)))               (vla-put-TextString att attval)))`
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 07:31:50 PM
You are getting closer, well done  :-)

The problem is that the list needs to be constructed using the proper functions, as it cannot be taken as a literal.

To be taken as a literal, (i.e. not evaluated), an apostrophe must be used, but, in your case, you cannot use this method as you need to evaluate the variables within your list.

The error occurs because elements of your list are interpreted as function expressions, e.g.

Code: [Select]
`("BOTANICAL" species)`
Is attempting to evaluate "BOTANICAL" as a function, which, of course, it is not.

To construct the list, look into using the list function, (and cons function if you want to use a dotted pair)
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 07:44:38 PM
ok, I think I have it now - a list of lists...

Code: [Select]
`(setq atlist (list         (list "ID" tnum)(list "BOTANICAL" species)(list "COMMON" common)(list "CULTIVAR" cultivar)(list "DBH" dbh)        (list "CONDITION" condition)(list "UNIQUEID" unique)(list "STEMS" stem)))`But this seems messy - would there be a better/cleaner way?
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 07:47:52 PM
Excellent - good job  :-)

Yes, it could be done with mapcar:

Code: [Select]
`(setq atlist   (mapcar 'list   '("ID" "BOTANICAL" "COMMON" "CULTIVAR" "DBH" "CONDITION" "UNIQUEID" "STEMS")    (list tnum species common cultivar dbh condition unique stem)  ))`
There may be also a cleaner way to obtain your original variables, perhaps making the above even more concise. :-)
Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 07:50:25 PM
Cool. Asside from looking cleaner, would MAPCAR run faster? That is one I still have some trouble grasping...

Well, again, thanks for your help and hints. Much appreciated :)
I still need to drive 2 hours to get home, so have a good weekend.

Title: Re: newbie question: COND limits?
Post by: CHulse on November 12, 2010, 07:53:14 PM
Using your STRBRK function, I have this:

Code: [Select]
`(WHILE (setq line (CAR *lst*))        (setq ;; method by FIXO @ CadTutor          tnum (ATOF (nth 0 line))          ;;common (nth 1 line)          species (nth 1 line)          cultivar (nth 2 line)          dbh (atof (nth 3 line))          condition (nth 4 line)          x (atof (nth 5 line))          y (ATOF (nth 6 line))          unique (nth 7 line)          stem (nth 8 line)          crz (ATOF (nth 9 line))          photo (nth 10 line)                  );_end of setq`
to establish the vars from a csv file input.
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 07:56:04 PM
Cool. Asside from looking cleaner, would MAPCAR run faster? That is one I still have some trouble grasping...

I doubt there would be much difference, perhaps mapcar may be slightly faster, but I would think the difference would be negligible.

As for understanding, here is a brief explanation I wrote a while back:

Well, again, thanks for your help and hints. Much appreciated :)
I still need to drive 2 hours to get home, so have a good weekend.

You're welcome - have a good weekend yourself too  :-)
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 12, 2010, 07:59:41 PM
Using your STRBRK function, I have this:

I can't give a complete answer, as I don't know how the variables are used elsewhere in the code, but, I would have thought that if you are dealing with attributes, you would want to deal in strings alone.

With that in mind, perhaps something similar to this may merge the two operations:

Code: [Select]
`(while (setq line (car *lst*))  (setq atlist (mapcar 'list '("ID" "BOTANICAL" ... ) line)))`
Lee
Title: Re: newbie question: COND limits?
Post by: MP on November 12, 2010, 08:10:08 PM
Some more food for thought (apologies in advance: I did not read the entire thread, hope the following fits):

Code: [Select]
`(setq vars '(tnum species cultivar dbh condition x y unique stem crz photo))(while (setq line (car *lst*))    (mapcar 'set vars line)        ;; perform any additional variable refinement    ;; I'm suggesting the distof function rather than atof        ;; however, I've not read the rest of the thread so its       ;; use may not necessarily be the most appropriate               ;; also note additional defensive coding may be applicable      ;; regardless distof or atof function usage                        (mapcar       '(lambda ( var ) (set var (distof (eval var))))       '(tnum dbh x y crz)    )        ;;  carry on and do other stuff        ;;  remember to modify the *lst* variable or you'll have an infinite loop        (setq *lst* (cdr *lst)))`
Title: Re: newbie question: COND limits?
Post by: Kerry on November 12, 2010, 08:18:10 PM

Quote
.. additional defensive coding may be applicable

now, there's a concept that could fill a couple of chapters. !

:)
Title: Re: newbie question: COND limits?
Post by: CAB on November 12, 2010, 11:50:53 PM
Another variation, for example only.
Code: [Select]
`  (setq vars '("ID" "BOTANICAL" "COMMON" "CULTIVAR" "DBH" "CONDITION" "UNIQUEID" "STEMS"))  (setq atlist           (mapcar '(lambda (var)                      (list var                            (cond                              ((vl-position var '("ID" "DBH" "STEMS"))                               (distof (eval (read var))))                              ((eval (read var)))                             ))) vars)  )`
Title: Re: newbie question: COND limits?
Post by: CHulse on November 14, 2010, 09:09:24 AM
Using your STRBRK function, I have this:

I can't give a complete answer, as I don't know how the variables are used elsewhere in the code, but, I would have thought that if you are dealing with attributes, you would want to deal in strings alone.

With that in mind, perhaps something similar to this may merge the two operations:

Code: [Select]
`(while (setq line (car *lst*))  (setq atlist (mapcar 'list '("ID" "BOTANICAL" ... ) line)))`
Lee

After you mentioned MAPCAR, I thought of something liike that, but not all the vars are used to populate attribs, x and y are a point, some are dynamic functions and one is a path for the hyperlink.

Thanks to everyone  - plenty of "food for thot" (if you like Dr John)...
It works as is, but I will continue to refine it just to help me learn some other methods.
Title: Re: newbie question: COND limits?
Post by: CHulse on November 14, 2010, 09:25:45 AM

Quote
.. additional defensive coding may be applicable

now, there's a concept that could fill a couple of chapters. !

:)

Ok, would someone like to explain this a bit more...? :)
Title: Re: newbie question: COND limits?
Post by: Lee Mac on November 14, 2010, 09:29:53 AM

Quote
.. additional defensive coding may be applicable

now, there's a concept that could fill a couple of chapters. !

:)

Ok, would someone like to explain this a bit more...? :)

http://en.wikipedia.org/wiki/Defensive_programming (http://en.wikipedia.org/wiki/Defensive_programming)
Title: Re: newbie question: COND limits?
Post by: MP on November 14, 2010, 09:50:52 AM
I would add that it's an art, and many programmers don't get it right, and what's "right" is a matter of opinion / taste. My basic stance is that low level routines should rarely, if ever, trap argument type errors, e.g., if a function expects a string and you pass it a real it should do a face plant and cause the calling function to chuck a wobbly, because that's a programming error. I see many programmers applying overly defensive strategies like this, and I think that's a mistake. However, higher level functions / procedures should trap potential errors, e.g. a function that processes csv data should anticipate that one or more fields are null or a data type not expected, because that's a potential execution error.

I acknowledge it can be a matter of taste, this was mine.

Man it's WAY to early for me to be posting. Runs off to find a coffee ...
Title: Re: newbie question: COND limits?
Post by: CAB on November 14, 2010, 01:23:01 PM
Most defensive programing for me is dealing with what the user might or might not do.
Back when I was doing some work in FoxBase & i thought I had all the options covered
I would let a friend of mine put the program through it's paces.
He could break anything & often did. :evil:
Title: Re: newbie question: COND limits?
Post by: John Kaul (Se7en) on November 14, 2010, 01:34:39 PM
`Defensive Programing'? I read that book when it was called "Forethought". The continuation of this discussion aught to be very interesting to say the lest.
Title: Re: newbie question: COND limits?
Post by: CHulse on November 15, 2010, 05:12:38 AM
...e.g. a function that processes csv data should anticipate that one or more fields are null or a data type not expected, because that's a potential execution error....

Ok, now you really have my attention. (it's 5 am here and I have coffee  :-D)
Any guidence on how I might address a null field? So far, I have just been very careful formatting the input data...
Title: Re: newbie question: COND limits?
Post by: Kerry on November 15, 2010, 05:48:11 AM

If unexpected null data, tell the user nicely, then spit the dummy.

Title: Re: newbie question: COND limits?
Post by: CAB on November 15, 2010, 08:51:25 AM
A more ambitious option would be to get input form the user for that data.
Use it this time and or update the data file. You would need to know if this user had permission to update the file.
One more option would be to check the file while loading for incorrect data & deal with it at that time.