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

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
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: 2140
  • 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...