TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jxphklibin on March 31, 2009, 03:16:45 AM

Title: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: jxphklibin on March 31, 2009, 03:16:45 AM
How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))

_$ (list 1245 "" nil "add" '(1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))
(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))

_$ (vl-list* 1 "" 2 NIL "" 3 5 "" NIL 6 "" 8  9)
(1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: Kerry on March 31, 2009, 04:57:23 AM
not pretty, but seems to work ..
Code: [Select]
(setq myList  (list 1245 "" nil "add" "" '(1 "" 2 nil "" 3 5 "" nil 6 "" 8.9))
      string  (vl-prin1-to-string myList)
      pattern (vl-prin1-to-string "")
)


(while (setq index (vl-string-search pattern string 0))
    (setq string (vl-string-subst "" pattern string))
)
(setq pattern (vl-prin1-to-string nil))
(while (setq index (vl-string-search pattern string 0))
    (setq string (vl-string-subst "" pattern string))
)

(setq NewList (read string))


;;=> (1245 "add" (1 2 3 5 6 8.9))
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: Kerry on March 31, 2009, 06:07:00 AM

oh, wait ..

[forestgump]
'pretty is as pretty does'
[/gump]

:)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: gile on March 31, 2009, 07:37:55 AM
Hi,

Another way

<EDIT>: corrected an error if some members of the last dotted pair have to be removed.

Code: [Select]
(defun nested-remove-if (fun lst / ele)
  (if lst
    (if (listp (cdr lst))
      (if (atom (setq ele (car lst)))
(if (apply fun (list ele))
  (nested-remove-if fun (cdr lst))
  (cons ele (nested-remove-if fun (cdr lst)))
)
(list (nested-remove-if fun ele))
      )
      (if (apply fun (list (car lst)))
(if (not (apply fun (list (cdr lst))))
  (cdr lst)
)
(if (apply fun (list (cdr lst)))
  (list (car lst))
  lst
)
      )
    )
  )
)

_$ (setq lst '(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))
(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))
_$ (n-remove-if '(lambda (x) (member x '("" nil))) lst)
(1245 "add" (1 2 3 5 6 8 . 9))
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: SomeCallMeDave on March 31, 2009, 08:02:45 AM
Gile's post gave the nudge I needed to get this one to work.  Merci, Gile.

Code: [Select]

(defun ShouldIGo(x)
     (if (member x '("" nil)) T nil)
)     

(setq myList '(1245 "" nil "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8.9)))     

(setq myNewList (vl-remove-if 'ShouldIGo (mapcar '(lambda (x) (if (listp x) (vl-remove-if 'ShouldIGo  x) x)) myList)))

Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: SomeCallMeDave on March 31, 2009, 08:04:50 AM
.... corrected an error if some members of the last dotted pair have to be removed.
...


Is that a dotted pair or a decimal number?

Mine will not work if it is a dotted pair
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: gile on March 31, 2009, 08:23:49 AM
Quote
Is that a dotted pair or a decimal number?

Mine will not work if it is a dotted pair

In jxphklibin's list the nested list is a dotted list built with vl-list*, it's why I built my own recursive function instead of using a mapcar statement (it's needed to test if the last cdr is an atom or a list)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: CAB on March 31, 2009, 08:58:02 AM
One would have to ask jxphklibin if the dotted list is your intention & for what pourpose.
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: jxphklibin on March 31, 2009, 08:59:37 AM
Nice , Kerry Brown , gile.
Thanks!


I just find a way to deal with it, no intention & no pourpose. Since there is possible existence of  the dotted list. At the same time, also let me know to delete the specified elements of the list (including nested list) in another way. You are very nice!Thanks again!!!
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: jxphklibin on March 31, 2009, 09:42:24 AM
Hi,

Another way

<EDIT>: corrected an error if some members of the last dotted pair have to be removed.

Code: [Select]
(defun nested-remove-if (fun lst / ele)
  (if lst
    (if (listp (cdr lst))
      (if (atom (setq ele (car lst)))
(if (apply fun (list ele))
  (nested-remove-if fun (cdr lst))
  (cons ele (nested-remove-if fun (cdr lst)))
)
(list (nested-remove-if fun ele))
      )
      (if (apply fun (list (car lst)))
(if (not (apply fun (list (cdr lst))))
  (cdr lst)
)
(if (apply fun (list (cdr lst)))
  (list (car lst))
  lst
)
      )
    )
  )
)

_$ (setq lst '(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))
(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))
_$ (n-remove-if '(lambda (x) (member x '("" nil))) lst)
(1245 "add" (1 2 3 5 6 8 . 9))

Hi,gile
When the list is:
 (list 1245 "" nil "add" "" (list 1 "" 2 nil "" 3 5 "" nil 6 "" 8 9 (list 888 888 "" nil 9999 9999 nil "") 555 "" nil 666) 777 "" nil 77777)
, then it doesn't work correctly! But Kerry Brown's work correctly!
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: gile on March 31, 2009, 09:53:22 AM
Oopss

Code: [Select]
(defun nested-remove-if (fun lst / ele)
  (if lst
    (if (listp (cdr lst))
      (if (atom (setq ele (car lst)))
(if (apply fun (list ele))
  (nested-remove-if fun (cdr lst))
  (cons ele (nested-remove-if fun (cdr lst)))
)
(cons (nested-remove-if fun ele)
      (nested-remove-if fun (cdr lst))
)
      )
      (if (apply fun (list (car lst)))
(if (not (apply fun (list (cdr lst))))
  (cdr lst)
)
(if (apply fun (list (cdr lst)))
  (list (car lst))
  lst
)
      )
    )
  )
)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: JohnK on March 31, 2009, 09:59:54 AM
Forgive me but may i ask why are you removing the "NIL" from the lists, can't you just ignore them?
I ask because it seems like removing them would take more effort then need be.
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 10:08:57 AM
Code: [Select]
(defun deflate ( x / a )
    (cond
        ((atom x) x)
        ((null (vl-consp x)) (if (vl-every 'atom x)(vl-remove-if 'null x)(mapcar 'deflate x)))
        ((setq a (car x)) (cons (deflate a) (deflate (cdr x))))
        ((deflate (cdr x)))
    )
)

Forgive me but may i ask why are you removing the "NIL" from the lists, can't you just ignore them?
I ask because it seems like removing them would take more effort then need be.

It's play time for nerds, get with the program.
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: JohnK on March 31, 2009, 10:14:03 AM
> It's play time for nerds, get with the program.
*lol*

Here is mine i guess.

Code: [Select]
(setq mylist '(list ((1245 "" ((nil "add" (""))))) (list 1 "" 2 nil "" 3 5 "" nil 6 "" 8 9 (list 888 888 "" nil 9999 9999 nil "") 555 "" nil 666 777 "" nil) 77777))

(killer-n mylist "")

(defun killer-n (x a)
    (if (atom x)
      (if (not (eq a x)) x)
      (vl-remove-if 'null
(cons (killer-n (car x) a)
            (killer-n (cdr x) a)))))
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: ElpanovEvgeniy on March 31, 2009, 10:23:20 AM
my variant:
Code: [Select]
(defun f (l)
 (cond ((null l) nil)
       ((atom l) (list l))
       ((listp l) (append (f (car l)) (f (cdr l))))
 ) ;_  cond
) ;_  defun
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: SomeCallMeDave on March 31, 2009, 10:23:26 AM
....
When the list is:
 (list 1245 "" nil "add" "" (list 1 "" 2 nil "" 3 5 "" nil 6 "" 8 9 (list 888 888 "" nil 9999 9999 nil "") 555 "" nil 666) 777 "" nil 77777)
.....

Scope creep   :-D

Back to my little drawing board
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 10:26:55 AM
Code: [Select]
(defun killer-n (x a)
    (if (atom x)
      (if (not (eq a x)) x)
      (vl-remove-if 'null
(cons (killer-n (car x) a)
            (killer-n (cdr x) a)))))

Thanks for 'getting w/the program', :lol:. Unfortunately it crashes when using the original poster's data. :(
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 10:28:40 AM
my variant:
Code: [Select]
(defun f (l)
 (cond ((null l) nil)
       ((atom l) (list l))
       ((listp l) (append (f (car l)) (f (cdr l))))
 ) ;_  cond
) ;_  defun

Very pretty Evgeniy :), but flattens the list instead of merely removing the nils :oops:.
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: ElpanovEvgeniy on March 31, 2009, 10:41:59 AM
I have not truly understood a problem, I am corrected...
Code: [Select]
(defun f (l)
 (if l
  (if (listp l)
   (if (car l)
    (cons (f (car l)) (f (cdr l)))
    (f (cdr l))
   ) ;_  if
   l
  ) ;_  if
 ) ;_  if
)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: ElpanovEvgeniy on March 31, 2009, 10:47:26 AM
variant 2:
Code: [Select]
(defun f (l)
 (cond ((not l) nil)
       ((atom l) l)
       ((and (listp l) (car l)) (cons (f (car l)) (f (cdr l))))
       ((f (cdr l)))
 ) ;_  cond
)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: JohnK on March 31, 2009, 10:48:15 AM

Thanks for 'getting w/the program', :lol:. Unfortunately it crashes when using the original poster's data. :(

*Ack* i didnt see that dot. I'll get back to it when i put out a fire or two.

Your right, nice puzzle.
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 10:51:34 AM
Very nicely done Evgeniy: both variants 1 & 2.
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 10:58:14 AM
variant 2:
Code: [Select]
(defun f (l)
 (cond ((not l) nil)
       ((atom l) l)
       ((and (listp l) (car l)) (cons (f (car l)) (f (cdr l))))
       ((f (cdr l)))
 ) ;_  cond
)

Can be reduced to:

Code: [Select]
(defun f (l)
    (cond
        ((null l) nil)
        ((atom l) l)
        ((car l) (cons (f (car l)) (f (cdr l))))
        ((f (cdr l)))
    )
)

:)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: ElpanovEvgeniy on March 31, 2009, 11:10:53 AM
Can be reduced to:

Code: [Select]
(defun f (l)
    (cond
        ((null l) nil)
        ((atom l) l)
        ((car l) (cons (f (car l)) (f (cdr l))))
        ((f (cdr l)))
    )
)

Very nicely reduced.
Excellent work!

:)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: CAB on March 31, 2009, 11:24:54 AM
Not only does the OP want to remove nil but also "" 8-)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 11:33:50 AM
Not only does the OP want to remove nil but also "" 8-)

lol, missed that salient point.

Evgeny's would be easy to modify to suit, but revising my own submission:

Code: [Select]
(defun deflate ( x / a )
    (cond
        ((atom x) x)
        ((null (vl-consp x)) (if (vl-every 'atom x)(vl-remove-if 'null x)(mapcar 'deflate x)))
        ((and (setq a (car x))(/= a "")) (cons (deflate a) (deflate (cdr x))))
        ((deflate (cdr x)))
    )
)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 11:49:34 AM
Here's a more generic function using Evgeny's submission as a starting point:

Code: [Select]
(defun remove_all ( x lst / a )
    (cond
        ((member x lst) nil)
        ((atom x) x)
        ((member (setq a (car x)) lst) (remove_all (cdr x) lst))
        ((cons (remove_all a lst) (remove_all (cdr x) lst)))
    )
)

(setq lst '(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))

(remove_all lst '(nil ""))

=> (1245 "add" (1 2 3 5 6 8 . 9))

I reserve the right to reverse the order of the arguments; at present doesn't feel right, but gotta get back to work.
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6
Post by: MP on March 31, 2009, 11:57:51 AM
Now named properly, and arguments in proper order (in my mind anyway): :ugly:

Code: [Select]
(defun remove_items ( items lst / a )
    (cond
        ((member lst items) nil)
        ((atom lst) lst)
        ((member (setq a (car lst)) items) (remove_items items (cdr lst)))
        ((cons (remove_items items a) (remove_items items (cdr lst))))
    )
)

(setq lst '(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))

(remove_items '(nil "") lst)

=> (1245 "add" (1 2 3 5 6 8 . 9))
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: gile on March 31, 2009, 12:06:59 PM
More generic, using a predicate function

Code: [Select]
(defun nest-remove-if (fun lst / a )
    (cond
        ((apply fun (list lst)) nil)
        ((atom lst) lst)
        ((apply fun (list (setq a (car lst)))) (nest-remove-if fun (cdr lst)))
        ((cons (nest-remove-if fun a) (nest-remove-if fun (cdr lst))))
    )
)

_$ (setq lst  (list 1245 "" nil "add" "" '(1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))
(1245 "" nil "add" "" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))
_$ (nest-remove-if '(lambda (x) (member x '("" nil))) lst)
(1245 "add" (1 2 3 5 6 8 . 9))
_$ (nest-remove-if 'numberp lst)
("" nil "add" "" ("" nil "" "" nil ""))



Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: Spike Wilbury on March 31, 2009, 12:18:38 PM
after seeing the last functions posted.... I start to remember the x-arch or reni urban site days.... they look to close (and I am not saying that were copied eh!) :)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: JohnK on March 31, 2009, 01:25:25 PM
yeah most of those are pretty killer. After taking care of some things here i dont even want to continue with mine (i would just embarrass myself).
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: kdub_nz on March 31, 2009, 10:23:31 PM
Nice , Kerry Brown , gile.
Thanks!


I just find a way to deal with it, no intention & no pourpose. Since there is possible existence of  the dotted list. At the same time, also let me know to delete the specified elements of the list (including nested list) in another way. You are very nice!Thanks again!!!

You're welcome jx'

Now that you have several answers, can you explain why you want to remove the items from the list.

Usually the items in a list are EXPECTED to be in a certain order .. with "" indicating a null string and nil indicating a null value.

It seemed to me at first thought that removing the items would ruin the possibility of any further referencing of items in the list.

Regards
Kerry Brown
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: jxphklibin on March 31, 2009, 11:32:13 PM
Nice , Kerry Brown , gile.
Thanks!


I just find a way to deal with it, no intention & no pourpose. Since there is possible existence of  the dotted list. At the same time, also let me know to delete the specified elements of the list (including nested list) in another way. You are very nice!Thanks again!!!

You're welcome jx'

Now that you have several answers, can you explain why you want to remove the items from the list.

Usually the items in a list are EXPECTED to be in a certain order .. with "" indicating a null string and nil indicating a null value.

It seemed to me at first thought that removing the items would ruin the possibility of any further referencing of items in the list.

Regards
Kerry Brown

I am sorry,this is my first meeting by my codes as follows:
Code: [Select]
;; By 木子CAD工具 小李子 2009-3-28
(defun AllBlkNInBlkdef (blkn / blkdef e typ bn bnl e-l nl)
   (setq blkdef (tblobjname "block" blkn))
   (while (setq e (entnext blkdef))
     (setq typ (cdr (assoc 0 (entget e))))
     (if (= typ "INSERT")
       (setq bn (vla-get-name (vlax-ename->vla-object e))
     bnl (if bn
   (cons ;;(if (setq nl (AllBlkNInBlkdef bn))
   (list bn nl)
   ;;bn
;;)
bnl
   )
)
)
     )
     (setq blkdef e)
   )
   bnl
)

(defun c:ABnInBl (/ i en ss blknlst ABblknlst)
   (if (setq ss (ssget '((0 . "INSERT"))))
     (repeat (setq i (sslength ss))
       (setq en (ssname ss (setq i (1- i)))
     blkn (vla-get-name (vlax-ename->vla-object en))
     blknlst (AllBlkNInBlkdef blkn)
     ABblknlst (cons ;;(if blknlst
(list blkn blknlst)
;;blkn
      ;;)
      ABblknlst
)
       )
     )
     (princ "*** 你没有选择任何图块 ***")
   )
  (princ ABblknlst)
  (princ)
)

Later, I modified the code as shown below, this situation does not appear.

Code: [Select]
;; By 木子CAD工具 小李子 2009-3-28
(defun AllBlkNInBlkdef (blkn / blkdef e typ bn bnl e-l nl)
   (setq blkdef (tblobjname "block" blkn))
   (while (setq e (entnext blkdef))
     (setq typ (cdr (assoc 0 (entget e))))
     (if (= typ "INSERT")
       (setq bn (vla-get-name (vlax-ename->vla-object e))
     bnl (if bn
   (cons (if (setq nl (AllBlkNInBlkdef bn))
   (list bn nl)
   bn
)
bnl
   )
)
)
     )
     (setq blkdef e)
   )
   bnl
)

(defun c:ABnInBl (/ i en ss blknlst ABblknlst)
   (if (setq ss (ssget '((0 . "INSERT"))))
     (repeat (setq i (sslength ss))
       (setq en (ssname ss (setq i (1- i)))
     blkn (vla-get-name (vlax-ename->vla-object en))
     blknlst (AllBlkNInBlkdef blkn)
     ABblknlst (cons (if blknlst
(list blkn blknlst)
blkn
         )
       ABblknlst
)
       )
     )
     (princ "*** 你没有选择任何图块 ***")
   )
  (princ ABblknlst)
  (princ)
)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: jxphklibin on March 31, 2009, 11:41:28 PM
Code was prepared, So I thought, if the nested list also includes a dotted list,and I try,it doesn't work. And then......:
Code: [Select]
(defun RemoveItem (item lst)
  (mapcar '(lambda (x) (if (vl-consp x) (RemoveItem item x) x))
          (vl-remove item lst)
  )
)

;; or
(defun RemoveItem (item lst)
   (mapcar '(lambda (x) (if (listp x) (RemoveItem item x) x))
           (vl-remove item lst)
   )
)

;; or:
(defun RemoveItem (item lst)
  (mapcar '(lambda (x) (if (atom x) x (RemoveItem item x)))
          (vl-remove item lst)
  )
)

;; and:

(defun RemoveItem (item lst / x tmplst)
  (foreach x (vl-remove item lst)
    (if (atom x)
      (setq tmplst (cons x tmplst))
      (setq tmplst (cons (RemoveItem item x) tmplst))
    )
  )
  (reverse tmplst)
)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: jxphklibin on April 01, 2009, 02:11:53 AM
Hi,gile

The efficiency of your function is too low, and the following two functions are the implementation of speed compared to the results from the comparison, it is obvious that the difference.

Code: [Select]
(defun nest-remove-if (fun lst / a )
    (cond
        ((apply fun (list lst)) nil)
        ((atom lst) lst)
        ((apply fun (list (setq a (car lst)))) (nest-remove-if fun (cdr lst)))
        ((cons (nest-remove-if fun a) (nest-remove-if fun (cdr lst))))
    )
)


(defun RemoveItem (pattern mylist / string index newlist)
  (setq string (vl-prin1-to-string myList)
pattern (vl-prin1-to-string pattern)
  )
  (while (setq index (vl-string-search pattern string 0))
    (setq string (vl-string-subst "" pattern string))
  )
  (read string)
  ;;(setq NewList (read string))
)

;; 测试效率
(defun c:test ( / lst t0)
  (setq lst (list 121 "" nil
  (list "abc" "" (list 777 888 '("aaa" 8888 nil "" 999 . 9) 999) "dde" "" 111)
  "aaa" "" 999 000
  (list 88 "" 9090 "" "" "AAA" "BBB" ""
(list "ccc" "" "DDD" "" 123 '( 222 "" nil 333 . 66666))
  )
  "" 55555555 nil "TTTTTT" '(44444 nil 585858 "" . 6666) "PPPPPPPP" "" nil "" "fffff" "" nil ""
     )
  )
 
  (setq t0 (getvar "TDUSRTIMER"))
  (repeat 1000
    (nest-remove-if '(lambda (x) (member x '("" nil))) lst)
  )
  (princ "\n用时")
  (princ (* (- (getvar "TDUSRTIMER") t0) 86400))
  (princ "秒")
 
  (setq t0 (getvar "TDUSRTIMER"))
  (repeat 1000
    (RemoveItem "" lst)
    (RemoveItem nil lst)
    ;;(foreach x '("" nil) (setq lst (RemoveItem x lst)))
  )
  (princ "\n用时")
  (princ (* (- (getvar "TDUSRTIMER") t0) 86400))
  (princ "秒")
 
  (princ)
)
;|
_$ (c:test)

用时8.375秒
用时0.344秒
_$ (c:test)

用时8.219秒
用时0.359秒
_$
|;
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: jxphklibin on April 01, 2009, 04:07:28 AM
Code: [Select]
(defun RemoveItem (pattern mylist / string index newlist)
  (setq string (vl-prin1-to-string myList)
pattern (vl-prin1-to-string pattern)
  )
  (while (setq index (vl-string-search pattern string 0))
    (setq string (vl-string-subst "" pattern string))
  )
  (read string)
  ;;(setq NewList (read string))
)


Oh, I just found that it should not deal with the LIST contains REAL number With vl-prin1-to-string :

(setq A pi)
(setq B (RemoveItem nil (list 1 nil A)))
(/= A (cadr B))--->T
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: cjw on April 02, 2009, 10:33:33 PM
More generic, using a predicate function

Code: [Select]
(defun nest-remove-if (fun lst / a )
    (cond
        ((apply fun (list lst)) nil)
        ((atom lst) lst)
        ((apply fun (list (setq a (car lst)))) (nest-remove-if fun (cdr lst)))
        ((cons (nest-remove-if fun a) (nest-remove-if fun (cdr lst))))
    )
)

_$ (setq lst  (list 1245 "" nil "add" "" '(1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))
(1245 "" nil "add" "" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))
_$ (nest-remove-if '(lambda (x) (member x '("" nil))) lst)
(1245 "add" (1 2 3 5 6 8 . 9))
_$ (nest-remove-if 'numberp lst)
("" nil "add" "" ("" nil "" "" nil ""))



Thanks for sharing,gile
I like it. "fun" agrument is funny! Good job!
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: ElpanovEvgeniy on April 03, 2009, 02:11:44 AM
only nil...
 :-)
Code: [Select]
(defun f (l)
    (cond
        ((atom l) l)
        ((car l) (cons (f (car l)) (f (cdr l))))
        ((f (cdr l)))
    )
)
Title: Re: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8
Post by: Andrea on April 06, 2009, 05:11:30 PM
my little contribution...

Code: [Select]
(while (vl-string-search "nil" (setq lt (vl-prin1-to-string l)))
(setq l (read (vl-string-subst "" "nil " lt)))
)

or..

Code: [Select]
(read (acet-str-replace "nil" "" (vl-prin1-to-string l) t))