Author Topic: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8  (Read 14237 times)

0 Members and 1 Guest are viewing this topic.

jxphklibin

  • Guest
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)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
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))
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>

oh, wait ..

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

:)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
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))
« Last Edit: March 31, 2009, 07:58:03 AM by gile »
Speaking English as a French Frog

SomeCallMeDave

  • Guest
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)))


SomeCallMeDave

  • Guest
.... 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

gile

  • Gator
  • Posts: 2507
  • Marseille, France
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)
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
One would have to ask jxphklibin if the dotted list is your intention & for what pourpose.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

jxphklibin

  • Guest
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!!!
« Last Edit: March 31, 2009, 09:33:25 AM by jxphklibin »

jxphklibin

  • Guest
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!

gile

  • Gator
  • Posts: 2507
  • Marseille, France
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
)
      )
    )
  )
)
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
> 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)))))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
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

SomeCallMeDave

  • Guest
....
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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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. :(
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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:.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
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
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
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
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605

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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Very nicely done Evgeniy: both variants 1 & 2.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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)))
    )
)

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
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!

:)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Not only does the OP want to remove nil but also "" 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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)))
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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))
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2507
  • Marseille, France
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 ""))



Speaking English as a French Frog

Spike Wilbury

  • Guest
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!) :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
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).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2121
  • class keyThumper<T>:ILazy<T>
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
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

jxphklibin

  • Guest
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)
)

jxphklibin

  • Guest
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)
)
« Last Edit: March 31, 2009, 11:48:30 PM by jxphklibin »

jxphklibin

  • Guest
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秒
_$
|;

jxphklibin

  • Guest
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

cjw

  • Guest
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!

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
only nil...
 :-)
Code: [Select]
(defun f (l)
    (cond
        ((atom l) l)
        ((car l) (cons (f (car l)) (f (cdr l))))
        ((f (cdr l)))
    )
)

Andrea

  • Water Moccasin
  • Posts: 2372
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))
« Last Edit: April 06, 2009, 05:39:00 PM by Andrea »
Keep smile...