+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Full Member
    Discipline
    Civil
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    27

    Default Insert list2 in list1 at certain position

    Registered forum members do not see this ad.

    I have 2 lists:
    List1 (0.0 -0.196738 0.0 0.196738 0.0 0.0)
    List2 (-0.059895 0.120221 -0.059895 0.0)

    I want to insert List2 in List1 at a certain position:
    NewList (0.0 -0.196738 0.0 -0.059895 0.120221 -0.059895 0.0 0.196738 0.0 0.0)
    In this case at position nth 3

    How do I do this?

  2. #2
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2015
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,153

    Default

    Hi,

    Just an example for you comprehend the process:

    Code:
    (setq list1 '(0.0 -0.196738 0.0 0.196738 0.0 0.0)
          list2 '(-0.059895 0.120221 -0.059895 0.0)
    )
    (setq position 3)
    (setq run 0)
    
    (foreach item list1
      (if (= (setq run (1+ run)) position)
        (foreach element list2
          (setq lst (cons element lst))
        )
        (setq lst (cons item lst))
      )
    )

  3. #3
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2015
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,153

    Default

    Another if you want to.

    Code:
    (setq pos 3)
    
    (mapcar '(lambda (u) (if (= (length lst) pos) (mapcar '(lambda (x) (setq lst (cons x lst))) list2)
                           (setq lst (cons u lst))))
            list1)

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Win 7 Pro / Win 10 Pro / Linux Mint
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2018
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,501

    Default

    For example:
    Code:
    (defun foo ( new pos lst / tmp )
        (repeat pos (setq tmp (cons (car lst) tmp) lst (cdr lst)))
        (append (reverse tmp) new lst)
    )
    Code:
    _$ (setq l1 '(0 1 2 3 4 5))
    (0 1 2 3 4 5)
    _$ (setq l2 '("a" "b" "c"))
    ("a" "b" "c")
    _$ (foo l2 3 l1)
    (0 1 2 "a" "b" "c" 3 4 5)
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  5. #5
    Senior Member
    Computer Details
    Stefan BMR's Computer Details
    Operating System:
    Win10
    Discipline
    Structural
    Using
    AutoCAD 2019
    Join Date
    Sep 2011
    Location
    Baia Mare, Romania
    Posts
    389

    Default

    A recursive solution
    Code:
    (defun inslist (l1 n l2 / f)
      (defun f (l n)
        (if
          (and l (> n 0))
          (cons (car l) (f (cdr l) (1- n)))
          (append l2 l)
        )
      )
      (f l1 n)
    )
    Code:
    (setq l1 '(0.0 -0.196738 0.0 0.196738 0.0 0.0)
          l2 '(-0.059895 0.120221 -0.059895 0.0)
    )
    
    (inslist l1 3 l2) -> (0.0 -0.196738 0.0 -0.059895 0.120221 -0.059895 0.0 0.196738 0.0 0.0)

  6. #6
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Win 7 Pro / Win 10 Pro / Linux Mint
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2018
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,501

    Default

    Or, without the subfunction -
    Code:
    (defun foo ( new pos lst )
        (if (and (< 0 pos) lst)
            (cons (car lst) (foo new (1- pos) (cdr lst)))
            (append new lst)
        )
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  7. #7
    Full Member
    Discipline
    Civil
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    27

    Default

    Registered forum members do not see this ad.

    Thanx, all examples work!

Similar Threads

  1. Replies: 63
    Last Post: 27th Jun 2016, 11:35 pm
  2. popup list2 depends on popup list1.
    By Brick_top in forum AutoLISP, Visual LISP & DCL
    Replies: 7
    Last Post: 9th Jan 2012, 02:28 pm
  3. Insert a block by lisp: _.insert or another way?
    By MarcoW in forum AutoLISP, Visual LISP & DCL
    Replies: 11
    Last Post: 3rd Nov 2010, 10:40 am
  4. I need insert text with position x y (E,N)
    By afritis in forum AutoCAD Drawing Management & Output
    Replies: 1
    Last Post: 23rd Jul 2006, 11:56 pm
  5. insert commad ( the object i insert are too big)
    By rami in forum AutoCAD Drawing Management & Output
    Replies: 1
    Last Post: 19th Oct 2005, 10:57 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts