Search code examples
lispautocadautolisp

Lisp - how to break a 'pair' of lists?


I am getting the following output (a separate pair of two lists) after running a chunk of code:

("a") ("a")

How do I combine this pair of lists into one? i.e. I want the following output:

("a" "a")

For clarification my code is:

(defun c:TEST ()
    (setq a (ssget)); I select a line here
    (setq b (assoc 8 (entget (ssname a 0)))); gets me all dotted pairs containing 8
    (setq c (list (cdr b))); gets second elements of each pair (strings in this case) and converts them to two separate lists
    (print c)
)

Solution

  • I have gone through your question, As far as I know, I am trying to solve your problem, it seems that you try to get layer name of that line.

    if you run test function as mention in question it returns ("a") ("a") twice because of the first one due to (print c) in code and the second one as function output value. the output is single list ("a") but it write two time in command prompt.

    I thought you misunderstood here. if you type (princ) at the end of your function then the output is ("a") .

    keep in mind AutoLISP always return the last statement of function as function value to get more go through developer documents.

    also, you can found in AutoLISP to print value developer use "print", "princ", "prin1" and "prompt" command for detail click here

    below I write some function which can help you to better understand the concept

    ;
    (defun c:TEST()
        (setq a (ssget))
        (setq b (assoc 8 (entget (ssname a 0))))
        (setq c (list (cdr b)))
        (print c)
      ;output-> ("a") ("a")
    )
    
    (defun c:TEST1 ()
        (setq a (ssget))
        (setq b (assoc 8 (entget (ssname a 0))))
        (setq c (list (cdr b)))
        (print c)
    
      (princ);return nothing
    
      ;output-> ("a")
    )
    
    
    (defun c:TEST2()
        (setq a (ssget))
        (setq b (assoc 8 (entget (ssname a 0))))
        (setq c (list (cdr b)))
        (print c)
    
      (princ "last statement");return nothing
      ;output-> ("a") last statement"last statement"
      )
    
    (defun c:TEST3()
        (setq a (ssget))
        (setq b (assoc 8 (entget (ssname a 0))))
        (setq c (list (cdr b)))
        (print c)
    
      (princ "last statement");return nothing
      (princ)
      ;output-> ("a") last statement
      )
    

    if you run test output is ("a") ("a")

    if you run test1 output is ("a")

    if you run test2 output is ("a") last statement"last statement"

    here you see "last statement" is two time because again same reason AutoLISP always return the last statement of function as function value

    if you run test3 output is ("a") last statement

    hope this helps