TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: subbup on June 29, 2004, 07:07:19 AM

Title: remove a item from list
Post by: subbup on June 29, 2004, 07:07:19 AM
I know the position of an item in a list.
Till that position I want to remove all the items.
Is there any function to do this.
Title: remove a item from list
Post by: hendie on June 29, 2004, 07:13:57 AM
have you looked into vl-remove ?
Title: remove a item from list
Post by: subbup on June 29, 2004, 07:27:03 AM
It's removing all the duplicates in a list.
but I want to delete upto particular position in a list.
Title: remove a item from list
Post by: Mark on June 29, 2004, 07:44:44 AM
this is as beautiful as some but.......
Code: [Select]

(setq lst (list 23 23 43 54 90 77 88 99))
(23 23 43 54 90 77 88 99)

; remove the first three elements
(repeat 2 (setq lst (vl-remove (nth 0 lst) lst)))
(54 90 77 88 99)
Title: remove a item from list
Post by: Jeff_M on June 29, 2004, 08:34:44 AM
Using the same list as Mark:
Code: [Select]

(defun list_trim (lst posit / )
  (repeat posit (setq lst (cdr lst)))
  lst
  )

_$ (setq lst (list 23 23 43 54 90 77 88 99))
(23 23 43 54 90 77 88 99)
_$ (list_trim lst 3)
(54 90 77 88 99)
_$ (list_trim lst 5)
(77 88 99)
_$ (list_trim lst 2)
(43 54 90 77 88 99)
_$ (list_trim lst 1)
(23 43 54 90 77 88 99)

BTW Mark, can you see why your code removes the first three elements of your list when you only run repeat twice?
Title: remove a item from list
Post by: CAB on June 29, 2004, 09:59:23 AM
Hay that's fun. :)
And useful too.
Code: [Select]

(defun list_Rtrim (lst posit / )
  (setq lst (reverse lst))
  (repeat posit (setq lst (cdr lst)))
  (reverse lst )
)

(defun list_Ltrim (lst posit / )
  (repeat posit (setq lst (cdr lst)))
)

(defun list_trim (lst left right / )
  (list_Ltrim (list_Rtrim lst right) left)
)



Code: [Select]

(setq lst (list 0 1 2 3 4 5 6 7 8 9))

_$ (list_ltrim lst 4)
(4 5 6 7 8 9)
_$ (list_rtrim lst 4)
(0 1 2 3 4 5)
_$ (list_trim lst 2 3)
(2 3 4 5 6)
Title: remove a item from list
Post by: SMadsen on June 29, 2004, 10:05:30 AM
Heh
.. and the recursive version?
Title: remove a item from list
Post by: CAB on June 29, 2004, 10:18:48 AM
You know that makes my head hurt. :oops:
Title: remove a item from list
Post by: CAB on June 29, 2004, 10:27:39 AM
Something like this?

Code: [Select]
(defun list_Ltrim (lst posit / )
  (if (> posit 1)
    (list_Ltrim (cdr lst) (1- posit))
    (cdr lst)
  )
)
Title: remove a item from list
Post by: SMadsen on June 29, 2004, 10:44:55 AM
Almost. What happens with (list_Ltrim alist 0)?

Or even (list_Ltrim alist -1)? I know that shouldn't be an issue but at least it shouldn't return (cdr alist)

I'd do it something like this:
Code: [Select]
(defun list_Ltrim (lst posit)
  (cond ((null lst) nil)
        ((> posit 0) (list_Ltrim (cdr lst) (1- posit)))
        (lst)
  )
)
Title: remove a item from list
Post by: CAB on June 29, 2004, 11:53:06 AM
Ah yes, the old fox does it again.
I like it, just wish i thought of it. :)
Title: remove a item from list
Post by: JohnK on June 29, 2004, 12:05:24 PM
*Psst* Look over here. (http://theswamp.org/phpBB2/viewtopic.php?t=635)
Title: remove a item from list
Post by: MP on June 29, 2004, 05:56:55 PM
I think what's been posted, particularly:

Code: [Select]

(defun list_Ltrim (lst posit)
  (cond ((null lst) nil)
        ((> posit 0) (list_Ltrim (cdr lst) (1- posit)))
        (lst)
  )
)

are very good solutions, and generally follow the approach I'd take.

But solely for fun, obfuscation and a demonstration of "thinking outside the box" consider why this works (code distilled to re-useable functions):

Code: [Select]

(defun MakeKeys ( lst / key )
    (setq key -1)
    (mapcar '(lambda (item) (setq key (1+ key))) lst)
)

(defun IndexList ( lst )
    (mapcar 'cons
        (MakeKeys lst)
        lst
    )
)

(defun LeftTrimList ( lst n / index )
    (if (< n 1) lst
        (mapcar 'cdr
            (member
                (assoc n (setq index (IndexList lst)))
                index
            )  
        )
    )
)

(setq lst '(A B C D E F G H I))

(LeftTrimList lst 5)  =>  (F G H I)

(LeftTrimList lst -1)  =>  (A B C D E F G H I)

(LeftTrimList lst 99)  =>  nil

Cheers. :)
Title: remove a item from list
Post by: SMadsen on June 29, 2004, 06:08:10 PM
Interesting approach, Michael. Quite a detour but instructive

So, a righttrim function would be
Code: [Select]
(defun RightTrimList (lst n / index)
  (if (< n 1) lst
    (reverse (mapcar 'cdr (member
                (assoc n (setq index (IndexList (reverse lst))))
                index))
    )
  )
)


Right?
Title: remove a item from list
Post by: MP on June 29, 2004, 06:11:38 PM
Quote from: SMadsen
Interesting approach, Michael. Quite a detour but instructive

Thank you Stig, just a knurd havin' fun. :)

<Didn't test your variant but it looks just about right ro me.>