Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Append a list to a list anywhere in the list

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Strydaris2492
386 Views, 9 Replies

Append a list to a list anywhere in the list

Hi everyone,

 

I am trying to combine 2 lists of points together and I can't seem to figure it out.

I know append will combine the lists together, but what I need is a second list to be combined after the first or second or third item in the list.

For Example,

(setq ListA '((A) (B) (C)) ) ;;First list of points

(setq ListB '((X) (Y) (Z)) ) ::Second list of points

Append gives me 

(append ListA ListB) = ((A) (B) (C) (X) (Y) (Z))

Cons gives me 

(cons ListA ListB) = (((A) (B) (C)) (X) (Y) (Z))

 

I found some code from the forums that sort of does what I need.

 

(defun add-item-to-list (listin newitem location / i tmp)
(and
(= (type listin) 'LIST)
(= (type location) 'INT)
(not (minusp location))
(setq i (length listin))
(>= i location)
(while (not (minusp (setq i (1- i))))
(setq tmp (cons (nth i listin) tmp))
(if (= i location)
    (setq tmp (cons newitem tmp))
   );i
  );w
);a
tmp
);d

 

The returned result is 

((A) ((X) (Y) (Z)) (B) (C)) which is a list within a list.

What I need is 

((A) (X) (Y) (Z) (B) (C)) or ((A) (B) (X) (Y) (Z) (C))

 

Can anyone help out with this?

9 REPLIES 9
Message 2 of 10

hey there,

without pre-checks

(defun add-item-to-list (listin newitem location / listout position)
  (setq index -1)
  (while (not position) 
	  (if (setq position (= (setq index (1+ index)) location))
	      	(setq listout (append listout newitem))
	        (setq listout (append listout (list (car listin)))
		      listin (cdr listin)
		)
	  )
  )
  (setq listout (append listout listin))
)
Message 3 of 10

Hi Komo,

 

thanks. You seem to always have a solution to my problems.

Appreciate all the help you lend out.

FYI the move dynamic block after insert still works great. It does get a little laggy if there is a lot of the same blocks inserted in the drawing.

 

Message 4 of 10
Kent1Cooper
in reply to: Strydaris2492

Here's one way:

 

(defun add-list-in-list (listOuter listInner after / tmp)
  (if
    (and
      (= (type listOuter) 'LIST)
      (= (type listInner) 'LIST)
      (= (type after) 'INT)
      (not (minusp after))
    ); and
    (progn
      (repeat after
        (setq
          tmp (cons (car listOuter) tmp)
          listOuter (cdr listOuter)
        ); setq
      ); repeat
      (append (reverse tmp) listInner listOuter)
    ); progn
  ); if
); defun

 

I used 'after' as an argument name rather than 'location,' because 'location' seems ambiguous -- if I give it [for example] 2, would that mean I want the added-in list placed so its first item is the 2nd thing in the result [like the positional argument in (substr)], or as the 3rd thing [like index number positions in (nth) and (ssname)]?

 

Uing your ListA and ListB, asking for the added list after 1 item in the base list, it produces:

 

Command: (add-list-in-list ListA ListB 1)
((A) (X) (Y) (Z) (B) (C))

 

You can use the length of the "outer" list as the 'after' argument to be equivalent to (append ListA ListB), or use 0 for the 'after' to put the "inner" list in its entirety onto the front of the "outer," equivalent to (append ListB ListA).

Kent Cooper, AIA
Message 5 of 10
Moshe-A
in reply to: Strydaris2492

@Strydaris2492  hi,

 

check this function

 

enjoy

Moshe

 

(defun append_list_in_list (ListA ListB p / i lst)
 (setq i -1)
 (foreach e ListA
  (setq i (1+ i))
  (if (= i p)
   (setq lst (append lst ListB (list e)))
   (setq lst (append lst (list e)))
  )
 ); foreach
  
 lst
)

 

Message 6 of 10
ВeekeeCZ
in reply to: Strydaris2492

If you have just a few members... write out the sequence one by one...

 

(list (nth 0 ListA) (nth 0 ListB) (nth 1 ListB) (nth 2 ListB) (nth 1 ListA) (nth 2 ListA))

 

Simple code, easy to understand. 

Message 7 of 10
john.uhden
in reply to: Strydaris2492

@Strydaris2492 ,

Here's what I have ben using for a couple of decades: 

  ;;-----------------------------------------------------------------------
  ;; John's "New and Improved" nthadd (09-18-01) using Herman's approach of
  ;; repeat instead of horrendously slow append:
  ;; data = list being inflated
  ;; item = item (or list of items) to add to data
  ;; n    = 'INT position in data list to add item (zero counter)
  ;; multiple = 1  = item is a list; add each element
  ;;          = nil = add item as is
  ;; incorporated with multiple option (09-28-01)
  ;; Kosterized (02-02-03)
  ;; (02-04-03) corrected for VEDIT
  (defun @nthadd (data item n multiple / len head)
     (and
        (listp data)
        (setq len (vl-list-length data))
        (= (type n) 'INT)
        (>= n 0)
        (setq n (min n len)) ; corrected from (<= n len)
        (progn
          (repeat n
             (setq head (cons (car data) head)
                   data (cdr data)
             )
          )
          (if (and multiple (listp item)(vl-list-length item))
             (foreach element (reverse item)(setq data (cons element data)))
             (setq data (cons item data))
          )
          (repeat n
             (setq data (cons (car head) data)
                   head (cdr head)
             )
          )
        )
     )
     data
  )

John F. Uhden

Message 8 of 10

great to know)

Message 9 of 10
_gile
in reply to: Strydaris2492

Hi,

 

From the List library at the bottom of this page.

 

(defun gc:insertRange (new ind lst)
  (cond
    ((null lst) new)
    ((zerop ind) (append new lst))
    ((cons (car lst) (gc:insertRange new (1- ind) (cdr lst))))
  )
)

 

 

_$ (gc:insertRange '("d" "e" "f") 3 '("a" "b" "c" "g" "h"))
("a" "b" "c" "d" "e" "f" "g" "h")

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 10 of 10

Thank you everyone.

Seems as though each one is an acceptable solution.

I went with @Kent1Cooper's solution.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report