Author Topic: List Clarification...  (Read 4297 times)

0 Members and 1 Guest are viewing this topic.

AWW

  • Guest
List Clarification...
« on: December 30, 2009, 11:33:06 AM »
Here is my code that I have:

(command "-layer" "C" (cadr a) (car a) "Lt" (caddr a) (car a) "Lw" (last a) (car a) "")

I know this:

car - returns 1st element of list
cadr - returns 2nd element of list
caddr - returns 3rd element of list
last - returns last element of list

What I am attempting to do is add another a 4th element to the list. So I would have 5 total elements in this line of code. I looked in the help file and didn't find what would return the 4th element of a list. Something to note: this reads a text file that is set up with all the standard layers I have. Does anyone know what would return the 4th element of a list?

rhino

  • Guest
Re: List Clarification...
« Reply #1 on: December 30, 2009, 11:43:26 AM »
Here is my code that I have:

(command "-layer" "C" (cadr a) (car a) "Lt" (caddr a) (car a) "Lw" (last a) (car a) "")

I can't seem to understand what your trying to achieve with that line of code but maybe this way will work for you:

Code: [Select]
(defun lay_man2 (/ lay_list lay_tbl vlay)
  (vl-load-com)
  (setvar "expert" 3)
  (command "_-linetype" "load" "center" "" "load" "hidden" "" "")
  (princ)
  (setq lay_tbl  (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        lay_list '(("BRACING" . 32)
                   ("CL" . 8)
   ("DIM" . 2)
                   ("GRID" . 9)
                   ("TEXT" . 4)
                   ("STEEL" . 3)
                   ("PPA" . 2)
                   ("HATCH" . 8)
                   ("WALL" . 8)
                  )
  )
  (foreach n lay_list
    (if (setq vlay (vla-add lay_tbl (car n)))
      (vla-put-color vlay (cdr n))
    )
  )
  (vlax-for n lay_tbl  (vla-put-LineWeight n 0))
  (vla-put-linetype (vla-item lay_tbl "GRID") "CENTER")
  (vla-put-linetype (vla-item lay_tbl "BRACING") "HIDDEN")

  (setvar "expert" 0)
  (princ)
) ; end lay_man 2 - using Visual Lisp Code

rhino

  • Guest
Re: List Clarification...
« Reply #2 on: December 30, 2009, 11:44:51 AM »
oh ya... :ugly:

Welcome to the Swamp! :D

Lee Mac

  • Seagull
  • Posts: 12285
  • London, England
Re: List Clarification...
« Reply #3 on: December 30, 2009, 11:45:01 AM »
The interpretter will accept up to 4 c----r terms, i.e.

Code: [Select]
c[color=blue]a[/color]r     c[color=blue]ad[/color]r     c[color=blue]aaa[/color]r     c[color=blue]aaaa[/color]r
c[color=blue]d[/color]r     c[color=blue]da[/color]r     c[color=blue]ddd[/color]r     c[color=blue]aaad[/color]r
        c[color=blue]aa[/color]r     c[color=blue]aad[/color]r     c[color=blue]aada[/color]r
        c[color=blue]dd[/color]r     c[color=blue]add[/color]r     c[color=blue]adaa[/color]r
                 c[color=blue]daa[/color]r     c[color=blue]daaa[/color]r
                 c[color=blue]dad[/color]r     c[color=blue]aadd[/color]r
                 c[color=blue]dda[/color]r     c[color=blue]adad[/color]r
                 c[color=blue]ada[/color]r     c[color=blue]daad[/color]r
                           c[color=blue]dada[/color]r
                           c[color=blue]adda[/color]r
                           c[color=blue]ddaa[/color]r
                           c[color=blue]addd[/color]r
                           c[color=blue]dadd[/color]r
                           c[color=blue]ddad[/color]r
                           c[color=blue]ddda[/color]r
                           c[color=blue]dddd[/color]r


You can construct them like this:

Code: [Select]
(c[color=red]a[/color]r  (c[color=red]d[/color]r lst))  =  (c[color=red]ad[/color]r lst)

(c[color=red]ad[/color]r (c[color=red]d[/color]r lst))  =  (c[color=red]add[/color]r lst)

(c[color=red]da[/color]r (c[color=red]d[/color]r lst))  =  (c[color=red]dad[/color]r lst)

More info here.

Also, you can use "nth" to access any element of a list:

Code: [Select]
(nth 0 lst) = (car   lst)
(nth 1 lst) = (cadr  lst)
(nth 2 lst) = (caddr lst)
...
(nth (1- (length lst)) lst) = (last lst)
...
etc
« Last Edit: December 30, 2009, 05:21:10 PM by Lee Mac »

AWW

  • Guest
Re: List Clarification...
« Reply #4 on: December 30, 2009, 12:14:23 PM »
Thanks for the welcome rhino. I came across the "nth" after I had posted. Obvisously didn't research deep enough. The "nth" seems like the simpler route to take. I checked out that link you gave Lee Mac. That's alot of variations. If I went the car cdr route, the 4th element would be cadddr, correct?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10369
Re: List Clarification...
« Reply #5 on: December 30, 2009, 12:59:46 PM »
My cheat sheet.
« Last Edit: December 30, 2009, 01:03:18 PM by CAB »
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

AWW

  • Guest
Re: List Clarification...
« Reply #6 on: December 30, 2009, 01:45:39 PM »
Thanks CAB! That helps alot. :-D

Lee Mac

  • Seagull
  • Posts: 12285
  • London, England
Re: List Clarification...
« Reply #7 on: December 30, 2009, 03:11:21 PM »
The "nth" seems like the simpler route to take. I checked out that link you gave Lee Mac. That's alot of variations. If I went the car cdr route, the 4th element would be cadddr, correct?

Not a problem AWW, I use c--r up to the 3rd or 4th level, but then nth becomes easier to read (but then it depends on the situation, nth is handy when you want to use a variable as the index argument).

The way to look at it with the c---r functions is that a "d" will "knock an element off the front", and an "a" will "retrieve the first element"

As an Example:

Code: [Select]
(setq lst  '[color=red]([/color]0 1 2 [color=blue]([/color]3 [color=green]([/color]4 5 6[color=green])[/color] 7 8 9[color=blue])[/color][color=red])[/color])

(c[color=blue]ad[/color]r lst)  =  1  ['[color=red]d[/color]' takes away the [color=red]0[/color], '[color=red]a[/color]' looks as the [color=red]1[/color]]

(c[color=blue]ddd[/color]r lst)  =  ((3 (4 5 6) 7 8 9))  [three '[color=red]d[/color]'s so knock off the first [color=red]3[/color] elements - [color=red]Note that its still nested[/color] ]

(c[color=blue]a[/color]r (c[color=blue]ddd[/color]r lst))  =  (c[color=blue]addd[/color]r lst)  =  (c[color=blue]a[/color]r '((3 (4 5 6) 7 8 9)))  =  (3 (4 5 6) 7 8 9)

(c[color=blue]a[/color]r (c[color=blue]ada[/color]r (c[color=blue]ddd[/color]r lst)))  =  (c[color=blue]aa[/color]dr (c[color=blue]addd[/color]r lst))  =  4

Just bear in mind that although the example with cdddr takes away the first three elements of the list, the returned list is still nested, as there is no 'a' to look at the first element of the nested list with the three taken away.

Hope this all makes sense.

Lee
« Last Edit: December 30, 2009, 09:37:33 PM by Lee Mac »