List Question

Discussion in 'AutoCAD' started by Shoukat, Dec 20, 2003.

  1. Shoukat

    Shoukat Guest

    Can anyone tell me how to remove an item from a list

    Thanks
    Shoukat
     
    Shoukat, Dec 20, 2003
    #1
  2. vl-remove Function
    Removes elements from a list
    (vl-remove element-to-remove list)

    Arguments
    element-to-remove
    The value of the element to be removed; may be any LISP data type.

    list
    Any list.

    Return Values
    The list with all elements except those equal to element-to-remove.

    Examples

    _$ (vl-remove pi (list pi t 0 "abc"))
    (T 0 "abc")

    or

    (vl-remove-if predicate-function list)
    Returns all elements of the supplied list that fail the test function

    (vl-remove-if-not predicate-function list)
    Returns all elements of the supplied list that pass the test function

    ----------------------------------------------------------------------
    ; for r14 and other needs:

    ;Tony Tanzillo
    (defun remove-at (lst pos / head)
    (repeat pos
    (setq head (cons (car lst) head)
    lst (cdr lst)
    )
    )
    (append (reverse head) (cdr lst))
    )

    ---

    ; for long list and high nth pos...

    (defun remove-at2 (lst pos / head len)
    (cond
    ( (> (/ (setq len (length lst)) 2) pos)
    (repeat pos
    (setq head (cons (car lst) head)
    lst (cdr lst)
    )
    )
    (append (reverse head) (cdr lst))
    )
    ( T
    (setq lst (reverse lst))
    (repeat (1- (- len pos))
    (setq head (cons (car lst) head)
    lst (cdr lst)
    )
    )
    (append (reverse (cdr lst)) head)
    )
    )
    )

    ; index = nth like
    ; newitm = new item to subst or nil to remove
    ; lst = original list
    ; r_lst = original list reversed
    ;
    ; Example: (setq rlist (reverse alist))
    ; (ALE_SUBST_NTH 2 nil alist rlist)

    (defun ALE_SUBST_NTH (index newitem lst r_lst / len olditem)
    (if (> (setq len (- (length lst) (1+ index))) -1)
    (progn
    (setq olditem (nth index lst))
    (while (/= index (length (setq r_lst (cdr (member olditem r_lst))))))
    (while (/= len (length (setq lst (cdr (member olditem lst))))))
    (append (reverse r_lst) (if newitem (list newitem) nil) lst)
    )
    lst
    )
    )


    ;
    ; removes all the duplicated elements leaving originals,
    ; faster on lists with low number of duplicates
    ; example:
    ; (setq alist2 (atoms-family 1))
    ; (setq alist2 (append alist2 alist2 alist2 alist2))
    ;
    (defun ALE_RemoveDupOnLst (InLst / OutLst)
    (reverse
    (foreach ForElm InLst
    (if (vl-position ForElm OutLst)
    OutLst
    (setq OutLst (cons ForElm OutLst))
    )
    )
    )
    )
    ;
    ; removes all the duplicated elements leaving originals,
    ; faster on lists with high number of duplicates
    ; example:
    ; (setq alst2 nil) (setq alist '(1 2 3 4 4 5 5 5 6 7 8 9 9 9 9))
    ; (setq alist2 (repeat 500 (setq alist2 (append alist2 alist))))
    ;
    (defun ALE_RemoveDupOnLst2 (InLst / OutLst CarElm)
    (while InLst
    (setq
    InLst (vl-remove (setq CarElm (car InLst)) (cdr InLst))
    OutLst (cons CarElm OutLst)
    )
    )
    (reverse OutLst)
    )
    ;
    ; removes all the duplicated elements leaving originals,
    ; for R14
    ;
    (defun ALE_RemoveDupOnLst14 (InLst / OutLst)
    (reverse
    (foreach ForElm InLst
    (if (member ForElm OutLst)
    OutLst
    (setq OutLst (cons ForElm OutLst))
    )
    )
    )
    )



    --
    ________________________________________________

    Marc'Antonio Alessi (TV) Italy
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    O.S. = XP Pro 2002 - Sp.1 - Ita
    AutoCAD = 2004 Ita - Sp.1
    ________________________________________________
     
    Marc'Antonio Alessi, Dec 20, 2003
    #2
  3. Shoukat

    John Uhden Guest

    There are a number of ways, depending on how you want to identify the item to
    remove:
    ;;-----------------------------------------------------------------
    ;; CUT_LIST.LSP (c)1995-96, John F. Uhden, CADvantage
    ;; This function deletes the first instance of an item from a list:
    ;; Corrected thanks to trouble shooting by Peter B. Tobey
    (defun @cv_cut_list (item items / m)
    (if (setq m (member item items))
    (progn
    (setq items (reverse items))
    (repeat (length m)(setq items (cdr items)))
    (append (reverse items) (cdr m))
    )
    items
    )
    )
    ;;**********************************************
    ;; NOTE: To cut all, use (vl-remove item items)
    ;;----------------------------------------------
    ;; Tony Tanzillo (01-25-02)
    (defun @cv_cut_nth (lst pos / head)
    (repeat pos
    (setq head (cons (car lst) head)
    lst (cdr lst)
    )
    )
    (append (reverse head) (cdr lst))
    )

    If the item might have some value that falls within a range or maybe matches a
    string pattern then you can...
    Command: (vl-remove-if '(lambda (x)(wcmatch (strcase x) "*A*"))'("It" "was" "a"
    "dark" "night"))
    ("It" "night")

    There's also a (vl-remove-if-not) function that works the same way.
     
    John Uhden, Dec 20, 2003
    #3
  4. What are you trying to do, remove every instance of an item or remove an
    item based on location like the nth function?

    Peter Jamtgaard
     
    Peter Jamtgaard, Dec 20, 2003
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.