Author Topic: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8  (Read 14374 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: 10637
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: 10637
> 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